def addKey(ontId, new_public_key, public_key, node_index=None, errorcode=47001, public_key_Array=[], errorkey="error"): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "method": "addKey", "address": "0300000000000000000000000000000000000000", "version": 1, "params": [ontId, new_public_key, public_key] } }, "RESPONSE": { errorkey: errorcode } } if (errorkey == "error_code"): request["SIGN_RESPONSE"] = {errorkey: errorcode} if node_index != None: request["NODE_INDEX"] = node_index else: node_index = Config.ontid_map[public_key] request["NODE_INDEX"] = node_index if len(public_key_Array) == 0 or len(public_key_Array) > 2: return API.contract().call_contract(Task(name="addKey", ijson=request), twice=True, sleep=2) else: return API.contract().call_multisig_contract( Task(name="addKey", ijson=request), public_key_Array[0], public_key_Array[1])
def invoke_transfer_neo(contract_address, new_admin_ontid, public_key="1", node_index = None): request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 0, "params": [ { "type": "string", "value": "transfer" }, { "type": "array", "value": [ { "type" : "string", "value" : contract_address }, { "type" : "bytearray", "value" : new_admin_ontid }, { "type" : "int", "value" : public_key } ] }, { "type": "array", "value": [ { "type" : "string", "value" : "" } ] } ] } }, "RESPONSE":{"error" : 0} } if node_index != None: request["NODE_INDEX"] = node_index return call_contract(Task(name="withdraw_user_role", ijson=request), twice = True)
def changeRecovery(ontId, new_recovery_address, old_recovery_address, public_key, node_index=None, errorcode=47001, old_recovery_address_Array=[]): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "method": "changeRecovery", "address": "0300000000000000000000000000000000000000", "version": 1, "params": [ontId, new_recovery_address, old_recovery_address] } }, "RESPONSE": { "error": errorcode } } if node_index != None: request["NODE_INDEX"] = node_index else: node_index = Config.ontid_map[public_key] request["NODE_INDEX"] = node_index if len(old_recovery_address_Array ) == 0 or len(old_recovery_address_Array) > 2: return API.contract().call_contract(Task(name="changeRecovery", ijson=request), twice=True) else: return multi_contract(Task(name="changeRecovery", ijson=request), old_recovery_address_Array[0], old_recovery_address_Array[1])
def getgenerateblocktime(self, param=None): task = Task(Config.BASEAPI_PATH + "/ws/getgenerateblocktime.json") task.set_type("ws") if param and isinstance(param, dict): taskrequest = task.request() for key in param: taskrequest[key] = param[key] task.set_request(taskrequest) return run_single_task(task)
def test_normal_039_postrawtx(self): try: (result, reponse) = API.contract().sign_transction( Task(testpath + "/resource/cli/siginvoketx.json"), False) rawtxdata = reponse["result"]["signed_tx"] action = "sendrawtransaction" version = "1.0.0" (process, response) = API.restful().postrawtx(rawtxdata, action, version) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def ws_heartbeat_thread(self, heartbeat_gap=5): while True: time.sleep(heartbeat_gap) try: if self.terminated: raise TestError("Error") self.LONG_LIVE_WS.send( json.dumps( Task(Config.BASEAPI_PATH + "/ws/heartbeat.json").data()["REQUEST"])) except Exception as e: logger.error(str(e.args[0])) return False
def invoke_function(contract_address, function_str, callerOntID, public_key="1", node_index=None): request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 1, "params": [{ "type": "string", "value": function_str }, { "type": "array", "value": [{ "type": "bytearray", "value": callerOntID }, { "type": "int", "value": public_key }] }, { "type": "array", "value": [{ "type": "string", "value": "" }] }] } }, "RESPONSE": { "error": 0 } } if node_index != None: request["NODE_INDEX"] = node_index return API.contract().call_contract(Task(name="invoke_function", ijson=request), twice=True)
def approve(contract_address, pay_address, get_address, amount, node_index=None, errorcode=47001, errorkey="error"): request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 1, "params": [{ "type": "string", "value": "approve" }, { "type": "array", "value": [{ "type": "bytearray", "value": script_hash_bl_reserver(base58_to_address(pay_address)) }, { "type": "bytearray", "value": script_hash_bl_reserver(base58_to_address(get_address)) }, { "type": "int", "value": amount }] }] } }, "RESPONSE": { "error": errorcode }, "NODE_INDEX": node_index } if (errorkey == "error_code"): request["SIGN_RESPONSE"] = {errorkey: errorcode} return call_contract(Task(name="approve", ijson=request), twice=True)
def transfer(contract_address, from_address, to_address, amount, node_index=None): request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 1, "params": [{ "type": "string", "value": "transfer" }, { "type": "string", "value": "" }, { "type": "array", "value": [{ "type": "bytearray", "value": script_hash_bl_reserver( base58_to_address(from_address)) }, { "type": "bytearray", "value": script_hash_bl_reserver(base58_to_address(to_address)) }, { "type": "int", "value": amount }] }] } }, "RESPONSE": { "error": 0 }, "NODE_INDEX": node_index } return call_contract(Task(name="transfer", ijson=request), twice=True)
def test_74_get_conn_count1(self): request = { "TYPE": "restful", "REQUEST": { "api": "/api/v1/get_conn_count1" }, "RESPONSE": { "Error": 0, } } task = Task(name="74_get_conn_count1", ijson=request) logger.open("restful/74_get_conn_count1.log", "74_get_conn_count1") (result, response) = run_single_task(task) logger.close(not result)
def test_75_no_url(self): request = { "TYPE": "restful", "REQUEST": { "api": "" }, "RESPONSE": { "Error": 0, } } task = Task(name="75_no_url", ijson=request) logger.open("restful/75_no_url.log", "75_no_url") (result, response) = run_single_task(task) logger.close(not result)
def init_admin(self, contract_address, admin_address, node_index=None, sleep=5): request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 1, "params": [{ "type": "string", "value": "init" }, { "type": "array", "value": [{ "type": "string", "value": "" }] }, { "type": "array", "value": [{ "type": "string", "value": "" }] }] } }, "RESPONSE": { "error": 0 } } if node_index != None: request["NODE_INDEX"] = node_index else: node_index = Config.ontid_map[admin_address] request["NODE_INDEX"] = node_index return self.call_contract(Task(name="init_admin", ijson=request), twice=True, sleep=sleep)
def delegate_user_role(self, contract_address, owner_user, delegate_user, delegate_role, period, level, public_key="1", node_index=None, errorcode=0, gas_price=Config.DEFAULT_GAS_PRICE, gas_limit=Config.DEFAULT_GAS_LIMIT, sleep=5): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": gas_price, "gas_limit": gas_limit, "address": "0600000000000000000000000000000000000000", "method": "delegate", "version": 0, "params": [ contract_address, owner_user, delegate_user, delegate_role, period, level, public_key ] } }, "RESPONSE": { "error": errorcode } } if node_index != None: request["NODE_INDEX"] = node_index else: node_index = Config.ontid_map[owner_user] request["NODE_INDEX"] = node_index return CONTRACT_API.call_contract(Task(name="delegate_user_role", ijson=request), twice=True, sleep=sleep)
def invoke_function_update(func_, param0, param1, param2, param3, param4, param5, param6, param7, errorcode=0, errorkey="error"): if (func_ == "updateConfig"): filename = "vbftConfig" else: filename = "globalParam" getStorageConf(filename) request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": "0700000000000000000000000000000000000000", "method": func_, "version": 0, "params": [ param0, param1, param2, param3, param4, param5, param6, param7 ] } }, "RESPONSE": { "error": errorcode } } if (errorkey == "error_code"): request["SIGN_RESPONSE"] = {errorkey: errorcode} (result, response) = API.contract().call_multisig_contract( Task(name="invoke_function_update", ijson=request), Config.AdminNum, Config.AdminPublicKeyList) time.sleep(5) getStorageConf(filename) return (result, response)
def setUpClass(cls): #for node_index in range(len(Config.NODES)): # stop_nodes([node_index]) #start_nodes([0, 1, 2, 3, 4, 5, 6], Config.DEFAULT_NODE_ARGS, True, True) #time.sleep(60) (cls.m_contractaddr_right, cls.m_txhash_right) = deploy_contract_full("tasks/A.neo", "name", "desc", 0) cls.m_txhash_wrong = "this is a wrong tx hash" cls.m_contractaddr_wrong = "this is a wrong address" (result, reponse) = RPCApi().getblockhash(height=1) cls.m_block_hash_right = reponse["result"] cls.m_block_hash_error = "this is a wrong block hash" cls.m_block_height_right = 1 cls.m_block_height_wrong = 9999 cls.m_block_height_overflow = 99999999 (result, reponse) = sign_transction(Task("tasks/cli/siginvoketx.json"), False) cls.m_signed_txhash_right = reponse["result"]["signed_tx"] cls.m_signed_txhash_wrong = "0f0f0f0f" + cls.m_signed_txhash_right cls.m_getstorage_contract_addr = "03febccf81ac85e3d795bc5cbd4e84e907812aa3" cls.m_getstorage_contract_addr_wrong = "5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c" cls.m_getstorage_contract_key = ByteToHex(b'key1') cls.m_getstorage_contract_value = ByteToHex(b'value1') invoke_function(cls.m_contractaddr_right, "put", "", "1", argvs=[{ "type": "bytearray", "value": cls.m_getstorage_contract_key }, { "type": "bytearray", "value": cls.m_getstorage_contract_value }], node_index=0) cls.getsmartcodeevent_height = 5 cls.getbalance_address_true = Config.NODES[0]["address"] cls.getbalance_address_false = "ccccccccccccccc" pass
def forNeo(contract_address,functionName,params,public_key, node_index = None, recovery_address_Array=[]): request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 1, "params": [ { "type": "string", "value": functionName }, { "type": "array", "value": params } ] } }, "RESPONSE":{"error" : 0} } if node_index != None: request["NODE_INDEX"] = node_index else: node_index = Common.ontid_map[public_key] request["NODE_INDEX"] = node_index if functionName == "changeRecovery": return multi_contract(Task(name="forNeo", ijson=request), recovery_address_Array[0],recovery_address_Array[1]) else: return call_contract(Task(name="forNeo", ijson=request), twice = True)
def allowance(contract_address,pay_address,get_address,node_index = None,errorcode=47001,errorkey="error"): if len(get_address)!=34 : send_get_address=ByteToHex(bytes(get_address, encoding = "utf8")) else: send_get_address=Common.bl_address(get_address) if len(pay_address)!=34 : send_pay_address=ByteToHex(bytes(pay_address, encoding = "utf8")) else: send_pay_address=Common.bl_address(pay_address) request = { "REQUEST": { "Qid": "t", "Method": "signeovminvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": contract_address, "version": 1, "params": [ { "type": "string", "value": "allowance" }, { "type": "array", "value": [ { "type": "bytearray", "value": send_pay_address }, { "type": "bytearray", "value": send_get_address } ] } ] } }, "RESPONSE":{"error" : errorcode}, "NODE_INDEX":node_index } if (errorkey =="error_code"): request["SIGN_RESPONSE"]={errorkey : errorcode} return API.contract().call_contract(Task(name="allowance", ijson=request), twice = True,sleep=0)
def setUpClass(cls): stop_all_nodes() start_nodes([0, 1, 2, 3, 4, 5, 6], Config.DEFAULT_NODE_ARGS, True, True) time.sleep(60) (cls.m_contractaddr_right, cls.m_txhash_right) = deploy_contract_full("tasks/A.neo", "name", "desc", 0) cls.m_txhash_wrong = "is a wrong tx hash" (result, reponse) = rpcApi.getblockhash(height=1) cls.m_block_hash_right = reponse["result"] cls.m_block_hash_error = "this is a wrong block hash" cls.m_block_height_right = 1 cls.m_block_height_wrong = 9999 cls.m_block_height_overflow = 99999999 (result, reponse) = sign_transction(Task("tasks/cli/siginvoketx.json"), False) cls.m_signed_txhash_right = reponse["result"]["signed_tx"] cls.m_signed_txhash_wrong = cls.m_signed_txhash_right + "0f0f0f0f" cls.m_getstorage_contract_addr = cls.m_contractaddr_right cls.m_getstorage_contract_addr_wrong = cls.m_contractaddr_right + "0f0f0f0f" cls.m_getstorage_contract_key = ByteToHex(b'key1') cls.m_getstorage_contract_value = ByteToHex(b'value1') invoke_function(cls.m_contractaddr_right, "put", "", "1", argvs=[{ "type": "bytearray", "value": cls.m_getstorage_contract_key }, { "type": "bytearray", "value": cls.m_getstorage_contract_value }], node_index=0) cls.getsmartcodeevent_height = 5 cls.getbalance_address_true = Config.NODES[0]["address"] cls.getbalance_address_false = "ccccccccccccccc"
def test_1(self): logger.open("TestNode.log", "TestNode") result = False try: node_i = 7 task = Task("tasks/transfer.json") nodeaddress = Config.NODES[node_i]["address"] task.data()["NODE_INDEX"] = node_i task.data()["REQUEST"]["Params"]["params"][1]["value"][0]["value"] = script_hash_bl_reserver(base58_to_address(nodeaddress)) task.data()["REQUEST"]["Params"]["params"][1]["value"][1]["value"] = script_hash_bl_reserver(base58_to_address(nodeaddress)) task.data()["REQUEST"]["Params"]["params"][1]["value"][2]["value"] = "1000" (result, response) = call_contract(task, twice = True) if not result: raise Error("call contract error") result = pause("please check you next step, then put '1' or '0' in you command window.\n '1' is ok, '0' is not ok") result = int(result) except Exception as e: print(e) print(e.msg) logger.close(result)
def test_abnormal_075_noUrl(self): try: request = { "TYPE" : "restful", "REQUEST": { "api": "" }, "RESPONSE": { "Error": 0, } } task = Task(name = "75_no_url", ijson = request) (process, response) = TaskRunner.run_single_task(task) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_074_getConnCount1(self): try: request = { "TYPE" : "restful", "REQUEST": { "api": "/api/v1/get_conn_count1" }, "RESPONSE": { "Error": 0, } } task = Task(name = "74_get_conn_count1", ijson = request) (process, response) = TaskRunner.run_single_task(task) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_main(self): logger.open("TestConsensus2.log", "TestConsensus2") try: #step 1 task1 = Task("tasks/invoke_put.json") (result, response) = call_contract(task1) if not result: raise Error("error") #step 2 if response['result']['Result String'] != "keytest": raise Error("key value can not get") except Exception as e: print(e.msg) logger.close(result)
def postrawtx(self, rawtxdata, action="sendrawtransaction", version="1.0.0", pre=0): task = Task(Config.BASEAPI_PATH + "/restful/post_raw_tx.json") task.set_type("restful") taskrequest = task.request() taskrequest["api"] = "/api/v1/transaction?preExec=" + str(pre) taskrequest["params"]["Action"] = action taskrequest["params"]["Version"] = version taskrequest["params"]["Data"] = rawtxdata task.set_request(taskrequest) return run_single_task(task)
def test_normal_069_get_version(self): try: task = Task(Config.BASEAPI_PATH + "/ws/getversion.json") task.set_type("ws") param = {"height": "abc"} if param and isinstance(param, dict): taskrequest = task.request() for key in param: taskrequest[key] = param[key] task.set_request(taskrequest) (process, response) = TaskRunner.run_single_task(task) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_69_get_version(self): log_path = "69_get_version.log" task_name = "69_get_version" self.start(log_path) task = Task(Config.BASEAPI_PATH + "/ws/getversion.json") task.set_type("ws") param = {"height": "abc"} if param and isinstance(param, dict): taskrequest = task.request() for key in param: taskrequest[key] = param[key] task.set_request(taskrequest) (result, response) = run_single_task(task) self.normal_finish(task_name, log_path, result, "")
def bind_role_function(self, contract_address, admin_address, role_str, functions, public_key="1", node_index=None, errorcode=0, gas_price=Config.DEFAULT_GAS_PRICE, gas_limit=Config.DEFAULT_GAS_LIMIT, sleep=5): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": gas_price, "gas_limit": gas_limit, "address": "0600000000000000000000000000000000000000", "method": "assignFuncsToRole", "version": 0, "params": [ contract_address, admin_address, role_str, functions, public_key ] } }, "RESPONSE": { "error": errorcode } } if node_index != None: request["NODE_INDEX"] = node_index else: node_index = Config.ontid_map[admin_address] request["NODE_INDEX"] = node_index return CONTRACT_API.call_contract(Task(name="bind_role_function", ijson=request), twice=True, sleep=sleep)
def genrator_transfer_sintx(): contrastaddress = deploy_contract("transfer.neo") sign_transction_transfer["REQUEST"]["Params"]["address"] = contrastaddress index = 1 sign_transction_transfertmp = copy.copy(sign_transction_transfer) nodeaddress = Config.NODES[index]["address"] sign_transction_transfertmp["NODE_INDEX"] = index sign_transction_transfertmp["REQUEST"]["Params"]["params"][1]["value"][0]["value"] = script_hash_bl_reserver(base58_to_address(nodeaddress)) sign_transction_transfertmp["REQUEST"]["Params"]["params"][1]["value"][1]["value"] = script_hash_bl_reserver(base58_to_address(nodeaddress)) sign_transction_transfertmp["REQUEST"]["Params"]["params"][1]["value"][2]["value"] = "1000" (result, response) = sign_transction(Task(ijson = sign_transction_transfertmp), False, False) print(json.dumps(response)) call_signed_contract(response["result"]["signed_tx"], pre=True) call_signed_contract(response["result"]["signed_tx"], pre=False)
def approve_31(neo_contract_address, from_address, to_address, amount): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "address": "0100000000000000000000000000000000000000", "method": "approve", "version": 1, "params": [from_address, to_address, amount] } }, "RESPONSE": {} } return call_contract(Task(name="test_1", ijson=request), twice=True)
def transferFrom_multi(self, put_address, amount, node_index=None, errorcode=0, public_key_Array=[], errorkey="error", gas_price=Config.DEFAULT_GAS_PRICE, gas_limit=Config.DEFAULT_GAS_LIMIT, sleep=5): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": gas_price, "gas_limit": gas_limit, "address": "0200000000000000000000000000000000000000", "method": "transferFrom", "version": 0, "params": [ put_address, "0100000000000000000000000000000000000000", put_address, amount ] } }, "RESPONSE": { errorkey: errorcode } } if (errorkey == "error_code"): request["SIGN_RESPONSE"] = {errorkey: errorcode} request["NODE_INDEX"] = node_index return CONTRACT_API.call_multisig_contract(Task( name="transferFrom_multi", ijson=request), public_key_Array[0], public_key_Array[1], sleep=sleep)
def subscribe(self, contractaddrlist, sevent=False, sjsonblock=False, srawblock=False, sblocktxhashs=False): task = Task(Config.BASEAPI_PATH + "/ws/subscribe.json") task.set_type("ws") taskrequest = task.request() taskrequest["ConstractsFilter"] = contractaddrlist taskrequest["SubscribeEvent"] = sevent taskrequest["SubscribeJsonBlock"] = sjsonblock taskrequest["SubscribeRawBlock"] = srawblock taskrequest["SubscribeBlockTxHashs"] = sblocktxhashs task.set_request(taskrequest) return run_single_task(task)