github.com/turingchain2020/turingchain@v1.1.21/build/docker-compose.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -e
     4  set -o pipefail
     5  #set -o verbose
     6  #set -o xtrace
     7  
     8  # os: ubuntu16.04 x64
     9  # first, you must install jq tool of json
    10  # sudo apt-get install jq
    11  # sudo apt-get install shellcheck, in order to static check shell script
    12  # sudo apt-get install parallel
    13  # ./docker-compose.sh build
    14  
    15  PWD=$(cd "$(dirname "$0")" && pwd)
    16  export PATH="$PWD:$PATH"
    17  
    18  NODE3="${1}_turingchain_1"
    19  CLI="docker exec ${NODE3} /root/turingchain-cli"
    20  
    21  NODE2="${1}_chain32_1"
    22  
    23  NODE1="${1}_chain31_1"
    24  
    25  NODE4="${1}_chain30_1"
    26  
    27  NODE5="${1}_chain29_1"
    28  CLI5="docker exec ${NODE5} /root/turingchain-cli"
    29  
    30  containers=("${NODE1}" "${NODE2}" "${NODE3}" "${NODE4}")
    31  export COMPOSE_PROJECT_NAME="$1"
    32  ## global config ###
    33  sedfix=""
    34  if [ "$(uname)" == "Darwin" ]; then
    35      sedfix=".bak"
    36  fi
    37  
    38  DAPP=""
    39  if [ -n "${2}" ]; then
    40      DAPP=$2
    41  fi
    42  
    43  DAPP_TEST_FILE=""
    44  if [ -n "${DAPP}" ]; then
    45      DAPP_TEST_FILE="testcase.sh"
    46      if [ -e "$DAPP_TEST_FILE" ]; then
    47          # shellcheck source=/dev/null
    48          source "${DAPP_TEST_FILE}"
    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  if [ -z "$DAPP" ]; then
    60      # shellcheck source=/dev/null
    61      source system-test-rpc.sh
    62  fi
    63  
    64  echo "=========== # env setting ============="
    65  echo "DAPP=$DAPP"
    66  echo "DAPP_TEST_FILE=$DAPP_TEST_FILE"
    67  echo "COMPOSE_FILE=$COMPOSE_FILE"
    68  echo "COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME"
    69  echo "CLI=$CLI"
    70  ####################
    71  
    72  testtoml=turingchain.toml
    73  # to close solo miner config in un-miner node
    74  testtomlsolo=turingchain-solo.toml
    75  
    76  function base_init() {
    77  
    78      # update test environment
    79      sed -i $sedfix 's/^Title.*/Title="local"/g' ${testtoml}
    80      sed -i $sedfix 's/^TestNet=.*/TestNet=true/g' ${testtoml}
    81  
    82      # p2p
    83      sed -i $sedfix 's/^seeds=.*/seeds=["turingchain:13802","chain32:13802","chain31:13802"]/g' ${testtoml}
    84      #sed -i $sedfix 's/^enable=.*/enable=true/g' turingchain.toml
    85      sed -i $sedfix '0,/^enable=.*/s//enable=true/' ${testtoml}
    86      sed -i $sedfix 's/^isSeed=.*/isSeed=true/g' ${testtoml}
    87      sed -i $sedfix 's/^innerSeedEnable=.*/innerSeedEnable=false/g' ${testtoml}
    88      sed -i $sedfix 's/^useGithub=.*/useGithub=false/g' ${testtoml}
    89  
    90      # rpc
    91      sed -i $sedfix 's/^jrpcBindAddr=.*/jrpcBindAddr="0.0.0.0:9671"/g' ${testtoml}
    92      sed -i $sedfix 's/^grpcBindAddr=.*/grpcBindAddr="0.0.0.0:9672"/g' ${testtoml}
    93      sed -i $sedfix 's/^whitelist=.*/whitelist=["localhost","127.0.0.1","0.0.0.0"]/g' ${testtoml}
    94  
    95      # wallet
    96      sed -i $sedfix 's/^minerdisable=.*/minerdisable=false/g' ${testtoml}
    97  
    98      #使能注册推送
    99      sed -i $sedfix 's/^enablePushSubscribe=.*/enablePushSubscribe=true/g' ${testtoml}
   100      sed -i $sedfix 's/^enableReduceLocaldb=.*/enableReduceLocaldb=false/g' ${testtoml}
   101  
   102      sed -i $sedfix 's/^enableTLS=.*/enableTLS=true/g' ${testtoml}
   103  
   104      cp ${testtoml} ${testtomlsolo}
   105      #consens
   106      consens_init "solo"
   107  
   108  }
   109  
   110  function consens_init() {
   111  
   112      if [ "$1" == "solo" ]; then
   113          sed -i $sedfix 's/^name="ticket"/name="solo"/g' ${testtoml}
   114          sed -i $sedfix 's/^singleMode=false/singleMode=true/g' ${testtoml}
   115  
   116          # only one node miner for solo miner
   117          sed -i $sedfix 's/^minerstart=true/minerstart=false/g' ${testtomlsolo}
   118      fi
   119  
   120  }
   121  function start() {
   122      echo "=========== # docker-compose ps ============="
   123      docker-compose ps
   124  
   125      # remove exsit container
   126      docker-compose down
   127  
   128      # create and run docker-compose container
   129      #docker-compose -f docker-compose.yml -f docker-compose-paracross.yml -f docker-compose-relay.yml up --build -d
   130      docker-compose up --build -d
   131  
   132      local SLEEP=5
   133      echo "=========== sleep ${SLEEP}s ============="
   134      sleep ${SLEEP}
   135  
   136      docker-compose ps
   137  
   138      # query node run status
   139      check_docker_status
   140      ${CLI} block last_header
   141      ${CLI} net info
   142  
   143      ${CLI} net peer
   144      peersCount=$(${CLI} net peer | jq '.[] | length')
   145      echo "peersCount=${peersCount}"
   146  
   147      echo "=========== # save seed to wallet ============="
   148      result=$(${CLI} seed save -p 1314fuzamei -s "tortoise main civil member grace happy century convince father cage beach hip maid merry rib" | jq ".isok")
   149      if [ "${result}" = "false" ]; then
   150          echo "save seed to wallet error seed, result: ${result}"
   151          exit 1
   152      fi
   153  
   154      sleep 1
   155  
   156      echo "=========== # unlock wallet ============="
   157      result=$(${CLI} wallet unlock -p 1314fuzamei -t 0 | jq ".isok")
   158      if [ "${result}" = "false" ]; then
   159          exit 1
   160      fi
   161  
   162      sleep 1
   163  
   164      echo "=========== # import private key returnAddr ============="
   165      result=$(${CLI} account import_key -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944 -l returnAddr | jq ".label")
   166      echo "${result}"
   167      if [ -z "${result}" ]; then
   168          exit 1
   169      fi
   170  
   171      sleep 1
   172  
   173      echo "=========== # import private key mining ============="
   174      result=$(${CLI} account import_key -k 4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01 -l minerAddr | jq ".label")
   175      echo "${result}"
   176      if [ -z "${result}" ]; then
   177          exit 1
   178      fi
   179  
   180      block_wait 1
   181  
   182      echo "=========== check genesis hash ========== "
   183      ${CLI} block hash -t 0
   184      res=$(${CLI} block hash -t 0 | jq ".hash")
   185      count=$(echo "$res" | grep -c "0xfd39dbdbd2cdeb9f34bcec3612735671b35e2e2dbf9a4e6e3ed0c34804a757bb")
   186      if [ "${count}" != 1 ]; then
   187          echo "genesis hash error!"
   188          exit 1
   189      fi
   190  
   191      echo "=========== query height ========== "
   192      ${CLI} block last_header
   193      result=$(${CLI} block last_header | jq ".height")
   194      if [ "${result}" -lt 1 ]; then
   195          block_wait 2
   196      fi
   197  
   198      #    sync_status "${CLI}"
   199  
   200      ${CLI} wallet status
   201      ${CLI} account list
   202      ${CLI} mempool list
   203  }
   204  
   205  function check_docker_status() {
   206      status=$(docker-compose ps | grep turingchain_1 | awk '{print $6}')
   207      if [ "${status}" == "Exit" ]; then
   208          echo "=========== turingchain service Exit logs ========== "
   209          docker-compose logs turingchain
   210          echo "=========== turingchain service Exit logs End========== "
   211      fi
   212  
   213  }
   214  
   215  function block_wait() {
   216      sleep "$1"
   217  
   218  }
   219  
   220  function block_wait_by_height() {
   221      if [ "$#" -lt 2 ]; then
   222          echo "wrong block_wait params"
   223          exit 1
   224      fi
   225  
   226      cur_height=$1
   227      # shellcheck disable=SC2004
   228      expect=$(($1 + $2))
   229      count=100
   230      while true; do
   231          new_height=$(${CLI} block last_header | jq ".height")
   232          if [ "${new_height}" -ge "${expect}" ]; then
   233              break
   234          fi
   235          count=$((count - 1))
   236          if [ $count -le 0 ]; then
   237              exit 1
   238          fi
   239          echo "wait new block, cur height=$new_height,expect=$expect"
   240          sleep 1
   241      done
   242      echo "wait new block remain $count s, cur height=$new_height,old=$cur_height"
   243  }
   244  
   245  function check_docker_container() {
   246      echo "============== check_docker_container ==============================="
   247      for con in "${containers[@]}"; do
   248          runing=$(docker inspect "${con}" | jq '.[0].State.Running')
   249          if [ ! "${runing}" ]; then
   250              docker inspect "${con}"
   251              echo "check ${con} not actived!"
   252              exit 1
   253          fi
   254      done
   255  }
   256  
   257  function sync_status() {
   258      echo "=========== query sync status========== "
   259      local sync_status
   260      local count=100
   261      local wait_sec=0
   262      while [ $count -gt 0 ]; do
   263          sync_status=$(${1} net is_sync)
   264          if [ "${sync_status}" = "true" ]; then
   265              break
   266          fi
   267          ((count--))
   268          wait_sec=$((wait_sec + 1))
   269          sleep 1
   270      done
   271      echo "sync wait  ${wait_sec} s"
   272  
   273      echo "=========== query clock sync status========== "
   274      sync_status=$(${1} net is_clock_sync)
   275      if [ "${sync_status}" = "false" ]; then
   276          exit 1
   277      fi
   278  }
   279  
   280  function sync() {
   281      echo "=========== stop  ${NODE5} node========== "
   282      docker stop "${NODE5}"
   283      sleep 10
   284  
   285      echo "=========== start ${NODE5} node========== "
   286      docker start "${NODE5}"
   287  
   288      sleep 1
   289      sync_status "${CLI5}"
   290  }
   291  
   292  function transfer() {
   293      echo "=========== # transfer ============="
   294      ${CLI} account balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e coins
   295      prebalance=$(${CLI} account balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e coins | jq -r ".balance")
   296  
   297      ${CLI} block last_header
   298      curHeight=$(${CLI} block last_header | jq ".height")
   299      echo "curheight=$curHeight"
   300      hashes=()
   301      for ((i = 0; i < 10; i++)); do
   302          hash=$(${CLI} send coins transfer -a 1 -n test -t 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944)
   303          echo "$hash"
   304          hashes=("${hashes[@]}" "$hash")
   305      done
   306  
   307      #send from 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt for test
   308      hash=$(${CLI} send coins transfer -a 10000 -n test -t 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944)
   309      echo "${hash}"
   310  
   311      block_wait_by_height "$curHeight", 1
   312  
   313      echo "len: ${#hashes[@]}"
   314      if [ "${#hashes[@]}" != 10 ]; then
   315          echo "tx number wrong"
   316          exit 1
   317      fi
   318      for ((i = 0; i < ${#hashes[*]}; i++)); do
   319          txs=$(${CLI} tx query_hash -s "${hashes[$i]}" | jq ".txs")
   320          if [ -z "${txs}" ]; then
   321              echo "cannot find tx"
   322              exit 1
   323          fi
   324      done
   325  
   326      ${CLI} account balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e coins
   327  
   328      local times=100
   329      while true; do
   330          newbalance=$(${CLI} account balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e coins | jq -r ".balance")
   331          echo "newbalance balance is ${newbalance}"
   332          balance=$(echo "$newbalance - $prebalance" | bc)
   333          echo "account added balance is ${balance}, expect 10.0000 "
   334          if [ "${balance}" != "10.0000" ]; then
   335              block_wait 2
   336              times=$((times - 1))
   337              if [ $times -le 0 ]; then
   338                  echo "account balance transfer failed, all tx list below:"
   339                  for ((i = 0; i < ${#hashes[*]}; i++)); do
   340                      echo "------the $i tx=${hashes[$i]}----------"
   341                      ${CLI} tx query_hash -s "${hashes[$i]}"
   342                  done
   343                  echo "----------block info------------------"
   344                  lastheight=$(${CLI} block last_header | jq -r ".height")
   345                  ${CLI} block get -s 1 -e "${lastheight}" -d 1
   346                  exit 1
   347              fi
   348          else
   349              echo "account balance transfer success"
   350              break
   351          fi
   352      done
   353  
   354      echo "=========== # add more blocks ============="
   355      # at least to height 3 for other test
   356      test_add_block
   357      test_add_block
   358  
   359  }
   360  
   361  function test_add_block() {
   362      echo "=========== # add one block ============="
   363      ${CLI} block last_header
   364      local height=0
   365      height=$(${CLI} block last_header | jq ".height")
   366      echo "curheight=$height"
   367  
   368      hash=$(${CLI} send coins transfer -a 1 -n test -t 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944)
   369      echo "$hash"
   370  
   371      block_wait_by_height "$height", 1
   372      ${CLI} tx query_hash -s "${hash}"
   373  }
   374  
   375  function base_config() {
   376      #    sync
   377      transfer
   378  }
   379  
   380  function base_test() {
   381      if [ "$DAPP" == "" ]; then
   382          system_test_rpc "https://${1}:9671"
   383      fi
   384      if [ "$DAPP" == "paracross" ]; then
   385          system_test_rpc "https://${1}:8901"
   386      fi
   387  }
   388  function dapp_run() {
   389      if [ -e "$DAPP_TEST_FILE" ]; then
   390          ${DAPP} "${CLI}" "${1}" "${2}"
   391      fi
   392  
   393  }
   394  function main() {
   395      echo "==============================DAPP=$DAPP main begin========================================================"
   396      ### init para ####
   397      base_init
   398      dapp_run init
   399  
   400      ### start docker ####
   401      start
   402  
   403      ### config env ###
   404      base_config
   405      dapp_run config
   406  
   407      ### test cases ###
   408      ip=$(${CLI} net info | jq -r ".externalAddr[0:10]")
   409      ip=$(echo "$ip" | cut -d':' -f 1)
   410      if [ "$ip" == "127.0.0.1" ]; then
   411          ip=$(${CLI} net info | jq -r ".localAddr")
   412          ip=$(echo "$ip" | cut -d':' -f 1)
   413      fi
   414      base_test "${ip}"
   415      dapp_run test "${ip}"
   416  
   417      ### finish ###
   418      check_docker_container
   419      echo "===============================DAPP=$DAPP main end========================================================="
   420  }
   421  
   422  # run script
   423  main