You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.0 KiB

  1. #! /bin/bash
  2. export GO111MODULE=on
  3. if [[ "$GRPC_BROADCAST_TX" == "" ]]; then
  4. GRPC_BROADCAST_TX=""
  5. fi
  6. set -u
  7. #####################
  8. # counter over socket
  9. #####################
  10. TESTNAME=$1
  11. # Send some txs
  12. function getCode() {
  13. set +u
  14. R=$1
  15. set -u
  16. if [[ "$R" == "" ]]; then
  17. echo -1
  18. fi
  19. if [[ $(echo $R | jq 'has("code")') == "true" ]]; then
  20. # this wont actually work if theres an error ...
  21. echo "$R" | jq ".code"
  22. else
  23. # protobuf auto adds `omitempty` to everything so code OK and empty data/log
  24. # will not even show when marshalled into json
  25. # apparently we can use github.com/golang/protobuf/jsonpb to do the marshalling ...
  26. echo 0
  27. fi
  28. }
  29. # build grpc client if needed
  30. if [[ "$GRPC_BROADCAST_TX" != "" ]]; then
  31. if [ -f grpc_client ]; then
  32. rm grpc_client
  33. fi
  34. echo "... building grpc_client"
  35. go build -mod=readonly -o grpc_client grpc_client.go
  36. fi
  37. function sendTx() {
  38. TX=$1
  39. set +u
  40. SHOULD_ERR=$2
  41. if [ "$SHOULD_ERR" == "" ]; then
  42. SHOULD_ERR=false
  43. fi
  44. set -u
  45. if [[ "$GRPC_BROADCAST_TX" == "" ]]; then
  46. RESPONSE=$(curl -s localhost:26657/broadcast_tx_commit?tx=0x"$TX")
  47. IS_ERR=$(echo "$RESPONSE" | jq 'has("error")')
  48. ERROR=$(echo "$RESPONSE" | jq '.error')
  49. ERROR=$(echo "$ERROR" | tr -d '"') # remove surrounding quotes
  50. RESPONSE=$(echo "$RESPONSE" | jq '.result')
  51. else
  52. RESPONSE=$(./grpc_client "$TX")
  53. IS_ERR=false
  54. ERROR=""
  55. fi
  56. echo "RESPONSE"
  57. echo "$RESPONSE"
  58. echo "$RESPONSE" | jq . &> /dev/null
  59. IS_JSON=$?
  60. if [[ "$IS_JSON" != "0" ]]; then
  61. IS_ERR=true
  62. ERROR="$RESPONSE"
  63. fi
  64. APPEND_TX_RESPONSE=$(echo "$RESPONSE" | jq '.deliver_tx')
  65. APPEND_TX_CODE=$(getCode "$APPEND_TX_RESPONSE")
  66. CHECK_TX_RESPONSE=$(echo "$RESPONSE" | jq '.check_tx')
  67. CHECK_TX_CODE=$(getCode "$CHECK_TX_RESPONSE")
  68. echo "-------"
  69. echo "TX $TX"
  70. echo "RESPONSE $RESPONSE"
  71. echo "ERROR $ERROR"
  72. echo "IS_ERR $IS_ERR"
  73. echo "----"
  74. if $SHOULD_ERR; then
  75. if [[ "$IS_ERR" != "true" ]]; then
  76. echo "Expected error sending tx ($TX)"
  77. exit 1
  78. fi
  79. else
  80. if [[ "$IS_ERR" == "true" ]]; then
  81. echo "Unexpected error sending tx ($TX)"
  82. exit 1
  83. fi
  84. fi
  85. }
  86. echo "... sending tx. expect no error"
  87. # 0 should pass once and get in block, with no error
  88. TX=00
  89. sendTx $TX
  90. if [[ $APPEND_TX_CODE != 0 ]]; then
  91. echo "Got non-zero exit code for $TX. $RESPONSE"
  92. exit 1
  93. fi
  94. echo "... sending tx. expect error"
  95. # second time should get rejected by the mempool (return error and non-zero code)
  96. sendTx $TX true
  97. echo "... sending tx. expect no error"
  98. # now, TX=01 should pass, with no error
  99. TX=01
  100. sendTx $TX
  101. if [[ $APPEND_TX_CODE != 0 ]]; then
  102. echo "Got non-zero exit code for $TX. $RESPONSE"
  103. exit 1
  104. fi
  105. echo "... sending tx. expect no error, but invalid"
  106. # now, TX=03 should get in a block (passes CheckTx, no error), but is invalid
  107. TX=03
  108. sendTx $TX
  109. if [[ "$CHECK_TX_CODE" != 0 ]]; then
  110. echo "Got non-zero exit code for checktx on $TX. $RESPONSE"
  111. exit 1
  112. fi
  113. if [[ $APPEND_TX_CODE == 0 ]]; then
  114. echo "Got zero exit code for $TX. Should have been bad nonce. $RESPONSE"
  115. exit 1
  116. fi
  117. echo "Passed Test: $TESTNAME"