github.com/turingchain2020/turingchain@v1.1.21/build/docker-compose-dapp.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  echo "=========== # env setting ============="
    60  echo "DAPP=$DAPP"
    61  echo "DAPP_TEST_FILE=$DAPP_TEST_FILE"
    62  echo "COMPOSE_FILE=$COMPOSE_FILE"
    63  echo "COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME"
    64  echo "CLI=$CLI"
    65  ####################
    66  
    67  function base_init() {
    68  
    69      # update test environment
    70      sed -i $sedfix 's/^Title.*/Title="local"/g' turingchain.toml
    71      sed -i $sedfix 's/^TestNet=.*/TestNet=true/g' turingchain.toml
    72  
    73      # p2p
    74      sed -i $sedfix 's/^seeds=.*/seeds=["turingchain:13802","chain32:13802","chain31:13802"]/g' turingchain.toml
    75      #sed -i $sedfix 's/^enable=.*/enable=true/g' turingchain.toml
    76      sed -i $sedfix '0,/^enable=.*/s//enable=true/' turingchain.toml
    77      sed -i $sedfix 's/^isSeed=.*/isSeed=true/g' turingchain.toml
    78      sed -i $sedfix 's/^innerSeedEnable=.*/innerSeedEnable=false/g' turingchain.toml
    79      sed -i $sedfix 's/^useGithub=.*/useGithub=false/g' turingchain.toml
    80  
    81      # rpc
    82      sed -i $sedfix 's/^jrpcBindAddr=.*/jrpcBindAddr="0.0.0.0:9671"/g' turingchain.toml
    83      sed -i $sedfix 's/^grpcBindAddr=.*/grpcBindAddr="0.0.0.0:9672"/g' turingchain.toml
    84      sed -i $sedfix 's/^whitelist=.*/whitelist=["localhost","127.0.0.1","0.0.0.0"]/g' turingchain.toml
    85  
    86      # wallet
    87      sed -i $sedfix 's/^minerdisable=.*/minerdisable=false/g' turingchain.toml
    88  
    89  }
    90  
    91  function start() {
    92      echo "=========== # docker-compose ps ============="
    93      docker-compose ps
    94  
    95      # remove exsit container
    96      docker-compose down
    97  
    98      # create and run docker-compose container
    99      #docker-compose -f docker-compose.yml -f docker-compose-paracross.yml -f docker-compose-relay.yml up --build -d
   100      docker-compose up --build -d
   101  
   102      local SLEEP=30
   103      echo "=========== sleep ${SLEEP}s ============="
   104      sleep ${SLEEP}
   105  
   106      docker-compose ps
   107  
   108      # query node run status
   109      ${CLI} block last_header
   110      ${CLI} net info
   111  
   112      ${CLI} net peer
   113      peersCount=$(${CLI} net peer | jq '.[] | length')
   114      echo "${peersCount}"
   115      if [ "${peersCount}" -lt 2 ]; then
   116          sleep 20
   117          peersCount=$(${CLI} net peer | jq '.[] | length')
   118          echo "${peersCount}"
   119          if [ "${peersCount}" -lt 2 ]; then
   120              echo "peers error"
   121              exit 1
   122          fi
   123      fi
   124  
   125      #echo "=========== # create seed for wallet ============="
   126      #seed=$(${CLI} seed generate -l 0 | jq ".seed")
   127      #if [ -z "${seed}" ]; then
   128      #    echo "create seed error"
   129      #    exit 1
   130      #fi
   131  
   132      echo "=========== # save seed to wallet ============="
   133      result=$(${CLI} seed save -p 1314fuzamei -s "tortoise main civil member grace happy century convince father cage beach hip maid merry rib" | jq ".isok")
   134      if [ "${result}" = "false" ]; then
   135          echo "save seed to wallet error seed, result: ${result}"
   136          exit 1
   137      fi
   138  
   139      sleep 1
   140  
   141      echo "=========== # unlock wallet ============="
   142      result=$(${CLI} wallet unlock -p 1314fuzamei -t 0 | jq ".isok")
   143      if [ "${result}" = "false" ]; then
   144          exit 1
   145      fi
   146  
   147      sleep 1
   148  
   149      echo "=========== # import private key returnAddr ============="
   150      result=$(${CLI} account import_key -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944 -l returnAddr | jq ".label")
   151      echo "${result}"
   152      if [ -z "${result}" ]; then
   153          exit 1
   154      fi
   155  
   156      sleep 1
   157  
   158      echo "=========== # import private key mining ============="
   159      result=$(${CLI} account import_key -k 4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01 -l minerAddr | jq ".label")
   160      echo "${result}"
   161      if [ -z "${result}" ]; then
   162          exit 1
   163      fi
   164  
   165      sleep 1
   166      echo "=========== # close auto mining ============="
   167      result=$(${CLI} wallet auto_mine -f 0 | jq ".isok")
   168      if [ "${result}" = "false" ]; then
   169          exit 1
   170      fi
   171  
   172      block_wait "${CLI}" 1
   173  
   174      echo "=========== check genesis hash ========== "
   175      ${CLI} block hash -t 0
   176      res=$(${CLI} block hash -t 0 | jq ".hash")
   177      count=$(echo "$res" | grep -c "0x67c58d6ba9175313f0468ae4e0ddec946549af7748037c2fdd5d54298afd20b6")
   178      if [ "${count}" != 1 ]; then
   179          echo "genesis hash error!"
   180          exit 1
   181      fi
   182  
   183      echo "=========== query height ========== "
   184      ${CLI} block last_header
   185      result=$(${CLI} block last_header | jq ".height")
   186      if [ "${result}" -lt 1 ]; then
   187          block_wait "${CLI}" 2
   188      fi
   189  
   190      sync_status "${CLI}"
   191  
   192      ${CLI} wallet status
   193      ${CLI} account list
   194      ${CLI} mempool list
   195  }
   196  
   197  function block_wait() {
   198      if [ "$#" -lt 2 ]; then
   199          echo "wrong block_wait params"
   200          exit 1
   201      fi
   202      cur_height=$(${1} block last_header | jq ".height")
   203      expect=$((cur_height + ${2}))
   204      count=0
   205      while true; do
   206          new_height=$(${1} block last_header | jq ".height")
   207          if [ "${new_height}" -ge "${expect}" ]; then
   208              break
   209          fi
   210          count=$((count + 1))
   211          sleep 1
   212      done
   213      echo "wait new block $count s, cur height=$expect,old=$cur_height"
   214  }
   215  
   216  function check_docker_container() {
   217      echo "============== check_docker_container ==============================="
   218      for con in "${containers[@]}"; do
   219          runing=$(docker inspect "${con}" | jq '.[0].State.Running')
   220          if [ ! "${runing}" ]; then
   221              docker inspect "${con}"
   222              echo "check ${con} not actived!"
   223              exit 1
   224          fi
   225      done
   226  }
   227  
   228  function sync_status() {
   229      echo "=========== query sync status========== "
   230      local sync_status
   231      local count=100
   232      local wait_sec=0
   233      while [ $count -gt 0 ]; do
   234          sync_status=$(${1} net is_sync)
   235          if [ "${sync_status}" = "true" ]; then
   236              break
   237          fi
   238          ((count--))
   239          wait_sec=$((wait_sec + 1))
   240          sleep 1
   241      done
   242      echo "sync wait  ${wait_sec} s"
   243  
   244      echo "=========== query clock sync status========== "
   245      sync_status=$(${1} net is_clock_sync)
   246      if [ "${sync_status}" = "false" ]; then
   247          exit 1
   248      fi
   249  }
   250  
   251  function sync() {
   252      echo "=========== stop  ${NODE5} node========== "
   253      docker stop "${NODE5}"
   254      sleep 10
   255  
   256      echo "=========== start ${NODE5} node========== "
   257      docker start "${NODE5}"
   258  
   259      sleep 1
   260      sync_status "${CLI5}"
   261  }
   262  
   263  function transfer() {
   264      echo "=========== # transfer ============="
   265      hashes=()
   266      for ((i = 0; i < 10; i++)); do
   267          hash=$(${CLI} send coins transfer -a 1 -n test -t 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt -k 4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01)
   268          hashes=("${hashes[@]}" "$hash")
   269      done
   270      block_wait "${CLI}" 1
   271      echo "len: ${#hashes[@]}"
   272      if [ "${#hashes[@]}" != 10 ]; then
   273          echo "tx number wrong"
   274          exit 1
   275      fi
   276  
   277      for ((i = 0; i < ${#hashes[*]}; i++)); do
   278          txs=$(${CLI} tx query_hash -s "${hashes[$i]}" | jq ".txs")
   279          if [ -z "${txs}" ]; then
   280              echo "cannot find tx"
   281              exit 1
   282          fi
   283      done
   284  
   285      echo "=========== # withdraw ============="
   286      hash=$(${CLI} send coins transfer -a 2 -n deposit -t 1wvmD6RNHzwhY4eN75WnM6JcaAvNQ4nHx -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944)
   287      echo "${hash}"
   288      block_wait "${CLI}" 1
   289      before=$(${CLI} account balance -a 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt -e retrieve | jq -r ".balance")
   290      if [ "${before}" == "0.0000" ]; then
   291          echo "wrong ticket balance, should not be zero"
   292          exit 1
   293      fi
   294  
   295      hash=$(${CLI} send coins withdraw -a 1 -n withdraw -e retrieve -k CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944)
   296      echo "${hash}"
   297      block_wait "${CLI}" 1
   298      txs=$(${CLI} tx query_hash -s "${hash}" | jq ".txs")
   299      if [ "${txs}" == "null" ]; then
   300          echo "withdraw cannot find tx"
   301          exit 1
   302      fi
   303  }
   304  
   305  function base_config() {
   306      sync
   307      transfer
   308  }
   309  
   310  function dapp_run() {
   311      if [ -e "$DAPP_TEST_FILE" ]; then
   312          ${DAPP} "${CLI}" "${1}"
   313      fi
   314  
   315  }
   316  function main() {
   317      echo "==============================DAPP=$DAPP main begin========================================================"
   318      ### init para ####
   319      base_init
   320      dapp_run init
   321  
   322      ### start docker ####
   323      start
   324  
   325      ### config env ###
   326      base_config
   327      dapp_run config
   328  
   329      ### test cases ###
   330      dapp_run test
   331  
   332      ### finish ###
   333      check_docker_container
   334      echo "===============================DAPP=$DAPP main end========================================================="
   335  }
   336  
   337  # run script
   338  main