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.

141 lines
2.9 KiB

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