function_verify.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright IBM Corp, All Rights Reserved.
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import print_function
  15. from hyperledger.client import Client
  16. # import base64
  17. import json
  18. import sys
  19. import time
  20. API_URL = 'http://127.0.0.1:5000'
  21. def query_value(chaincode_name, arg_list):
  22. """
  23. Query a list of values.
  24. :param chaincode_name: The name of the chaincode.
  25. :param arg_list: List of arguments.
  26. :return: A list of values.
  27. """
  28. result, resp = [], {}
  29. print("Query value will try at most 20 times.")
  30. for arg in arg_list:
  31. for i in range(20):
  32. try:
  33. resp = c.chaincode_query(chaincode_name=chaincode_name,
  34. function="query",
  35. args=[arg])
  36. if resp['result']['status'] == 'OK':
  37. result.append(resp['result']['message'])
  38. break
  39. except KeyError:
  40. print("Wait 1 seconds for the {0} query".format(i))
  41. time.sleep(1)
  42. return result
  43. # Usage:
  44. # * python function_test.py [API_URL=http://127.0.0.1:5000] will deploy first
  45. # * python function_test.py [API_URL=http://127.0.0.1:5000] [chaincode_name]
  46. # E.g.,
  47. # "f389486d91f54d1f8775940f24b1d3bd9f8a8e75d364e158ac92328ddacad629607a3c42be156fc4a7da7173adca2ac7d7eef29afc59c6f07f3ad14abee34f68"
  48. if __name__ == '__main__':
  49. if len(sys.argv) not in [2, 3]:
  50. print("Usage: python function_test.py ["
  51. "API_URL=http://127.0.0.1:5000] [chaincode_name]")
  52. exit()
  53. API_URL = sys.argv[1]
  54. chaincode_name = ""
  55. if len(sys.argv) == 3:
  56. chaincode_name = sys.argv[2]
  57. c = Client(base_url=API_URL)
  58. print("Checking cluster at {}".format(API_URL))
  59. if not chaincode_name:
  60. print(">>>Test: deploy the default chaincode")
  61. res = c.chaincode_deploy(args=["a", "10000", "b", "20000"])
  62. chaincode_name = res['result']['message']
  63. assert res['result']['status'] == 'OK'
  64. print("Successfully deploy chaincode with returned name = " +
  65. chaincode_name)
  66. print("Wait 15 seconds to make sure deployment is done.")
  67. time.sleep(15)
  68. print(">>>Check the initial value: a, b")
  69. values = query_value(chaincode_name, ["a", "b"])
  70. print(values)
  71. assert values == ['10000', '20000']
  72. print(">>>Test: invoke a chaincode: a-->b 1")
  73. res = c.chaincode_invoke(chaincode_name=chaincode_name, function="invoke",
  74. args=["a", "b", "1"])
  75. assert res["result"]["status"] == "OK"
  76. transaction_uuid = res["result"]["message"]
  77. print("Transaction id = {0}".format(transaction_uuid))
  78. # TODO: sleep 3 seconds till invoke done.
  79. print("Wait 5 seconds to make sure invoke is done.")
  80. time.sleep(5)
  81. print(">>>Check the after value: a, b")
  82. values = query_value(chaincode_name, ["a", "b"])
  83. print(values)
  84. assert values == ['9999', '20001']
  85. time.sleep(5)
  86. print(">>>Test: Check the transaction content")
  87. res = c.transaction_get(transaction_uuid)
  88. # res["chaincodeID"] = base64.b64decode(res["chaincodeID"])
  89. print(json.dumps(res, sort_keys=True, indent=4))
  90. assert res["uuid"] == transaction_uuid
  91. print(">>>Test: list the peers")
  92. res = c.peer_list()
  93. print(json.dumps(res, sort_keys=True, indent=4))
  94. assert len(res['peers']) > 0
  95. print(">>>Test: list the chain")
  96. res = c.chain_list()
  97. print(json.dumps(res, sort_keys=True, indent=4))
  98. assert res['height'] > 0
  99. print("Existing block number = {0}".format(res["height"]))
  100. print(">>>Test: get the content of block 1")
  101. res = c.block_get(block='1')
  102. print(json.dumps(res, sort_keys=True, indent=4))
  103. print(">>>Test: get the content of block 2")
  104. res = c.block_get(block='2')
  105. print(json.dumps(res, sort_keys=True, indent=4))