def test_abnormal_002_heartbeat(self): try: API.node().stop_node(0) time.sleep(5) (process, response) = API.ws().heartbeat() API.node().start_node(0, Config.DEFAULT_NODE_ARGS) time.sleep(10) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def setUp(self): logger.open("test_restful/" + self._testMethodName + ".log", self._testMethodName) if self._testMethodName == "test_init": return API.node().stop_all_nodes() API.node().start_nodes([0, 1, 2, 3, 4, 5, 6], Config.DEFAULT_NODE_ARGS, True, True) time.sleep(10)
def start_nodes(self): logger.info("start all nodes ontology and sigsvr") API.node().start_nodes(list(range(len(Config.NODES))), start_params=self.default_node_args, clear_chain=True, clear_log=True) API.node().start_sigsvrs(self.wallet_path, list(range(len(Config.NODES)))) logger.info("waiting for 10 seconds......") time.sleep(10)
def test_init(self): time.sleep(2) print("stop all") API.node().stop_all_nodes() print("start all") API.node().start_nodes([0,1,2,3,4,5,6], Config.DEFAULT_NODE_ARGS, True, True) time.sleep(60) print("waiting for 60s......") (test_config.contract_addr, test_config.contract_tx_hash) = API.contract().deploy_contract_full(testpath+"/resource/test.neo") API.node().wait_gen_block() test_config.CONTRACT_ADDRESS_CORRECT = test_config.contract_addr test_config.CONTRACT_ADDRESS_INCORRECT_2 = test_config.contract_addr + "11" test_config.TX_HASH_CORRECT = test_config.contract_tx_hash test_config.TX_HASH_INCORRECT_2 = test_config.contract_tx_hash[:-2] test_config.TX_HASH_INCORRECT_3 = test_config.contract_tx_hash + "1111" test_config.block_height = int(API.rpc().getblockcount()[1]["result"]) - 1 test_config.block_hash = API.rpc().getblockhash(test_config.block_height - 1)[1]["result"] test_config.signed_data = get_signed_data() test_config.HEIGHT_CORRECT = test_config.block_height - 1 test_config.HEIGHT_INCORRECT_2 = test_config.block_height + 1000 test_config.BLOCK_HASH_CORRECT = test_config.block_hash test_config.BLOCK_HASH_INCORRECT_2 = test_config.block_hash[:-2] # HASH NOT EXISTENT test_config.BLOCK_HASH_INCORRECT_3 = test_config.block_hash + "1111" test_config.RAW_TRANSACTION_DATA_CORRECT = test_config.signed_data test_config.RAW_TRANSACTION_DATA_INCORRECT_2 = "11111111" + test_config.signed_data + "1111111111"
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 test_normal_025_getconnectioncount(self): try: API.node().stop_nodes([1, 2, 3, 4, 5, 6]) (process, response) = API.rpc().getconnectioncount() API.node().start_nodes([1, 2, 3, 4, 5, 6], Config.DEFAULT_NODE_ARGS, False, False) time.sleep(10) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_06_getblock(self): try: bestblockhash = API.rpc().getbestblockhash() result = API.rpc().getblock(bestblockhash, 1) self.ASSERT(result, "") except AssertError as e: logger.error(e.msg) except RPCError as e: logger.error(e.msg) self.ASSERT(True, "") except Exception as e: logger.error(traceback.format_exc())
def test_base_050_changeRecovery(self): try: (process, response) = addRecovery(test_config.userOntId6, test_config.recoveryaddress,test_config.public_key,test_config.node_index,0)#以防万一,加一个 if process: API.node().wait_gen_block() (process, response) = changeRecovery(test_config.userOntId6, test_config.new_recovery_address,test_config.old_recovery_address,test_config.public_key,test_config.node_index,0,test_config.old_recovery_address_Array)#先把它换成新�? if process: API.node().wait_gen_block() (process1, response) = changeRecovery(test_config.userOntId6, test_config.old_recovery_address,test_config.new_recovery_address,test_config.public_key,test_config.node_index,0,test_config.new_recovery_address_Array)#再把它换回来 self.ASSERT(process, "") except Exception as e: logger.print (e.args[0])
def test_normal_092_removeAttribute(self): try: (process, response) = addAttributes(test_config.ontId, test_config.attributes_array,test_config.public_key,test_config.node_index,0)#1加进去 if process: API.node().wait_gen_block() (process, response) = removeAttribute(test_config.ontId, test_config.delAttriPath1,test_config.public_key,test_config.node_index,0)#2把attribute删了 if process: API.node().wait_gen_block() (process, response) = addAttributes(test_config.ontId, test_config.attributes_array,test_config.public_key,test_config.node_index,0)#3再加进去 self.ASSERT(process, "") except Exception as e: logger.print (e.args[0])
def test_normal_015_addKey(self): try: (process,response) = regIDWithPublicKey(test_config.menualOntId, test_config.public_key,test_config.node_index,0) if process: API.node().wait_gen_block() (process, response) = addKey(test_config.menualOntId, test_config.new_publickey3,test_config.pubkey_re_address,test_config.node_index,0)#先在其他用户处注册完毕该公钥 if process: API.node().wait_gen_block() (process, response) = addKey(test_config.ontId, test_config.new_publickey3,test_config.pubkey_re_address,test_config.node_index,0) self.ASSERT(process, "") except Exception as e: logger.print (e.args[0])
def changeRecovery(ontId, new_recovery_address, old_recovery_address, public_key, node_index=None, errorcode=47001, old_recovery_address_Array=[], errorkey="error"): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "method": "changeRecovery", "address": "0300000000000000000000000000000000000000", "version": 1, "params": [ ontId, Common.bl_reserver(new_recovery_address), Common.bl_reserver(old_recovery_address) ] } }, "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(old_recovery_address_Array ) == 0 or len(old_recovery_address_Array) > 2: return API.contract().call_contract(Task(name="changeRecovery", ijson=request), twice=True, sleep=2) else: return API.contract().call_multisig_contract( Task(name="changeRecovery", ijson=request), old_recovery_address_Array[0], old_recovery_address_Array[1])
def test_base_033_consensus(self): process = False try: test_api.add_candidate_node(7, init_pos=2000, from_node=0) test_api.getStorageConf("vbftConfig") # step 2 wallet A unvote in the second round (process, response) = API.native().commit_dpos() API.node().wait_gen_block() test_api.getStorageConf("vbftConfig") self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def stop_nodes(self): logger.info("stop all nodes ontology and sigsvr") API.node().stop_all_nodes() API.node().stop_sigsvrs(list(range(len(Config.NODES)))) for i in range(len(Config.NODES)): API.node().exec_cmd("killall sigsvr", i) API.node().exec_cmd("killall sigsvr-linux", i)
def test_base_001_gover(self): process = False try: (wallet_A_address, wallet_B_address, vote_price, node_B_puiblic_key, blocks_per_round, punish_ratio) = test_api.get_config() # step 1 before vote get balance of wallet A B print("*******", API.rpc().getbalance(wallet_A_address)) balance_of_wallet_A_1 = int(API.rpc().getbalance(wallet_A_address)[1]["result"]["ont"]) balance_of_wallet_B_1 = int(API.rpc().getbalance(wallet_B_address)[1]["result"]["ont"]) # step 2 wallet A vote for node B (process, response) = API.native().vote_for_peer(wallet_A_address, [node_B_puiblic_key], [vote_price], 8) self.ASSERT(process, "vote_for_peer error") API.node().wait_gen_block() # step 3 after vote get balance of wallet A B balance_of_wallet_A_2 = int(API.rpc().getbalance(wallet_A_address)[1]["result"]["ont"]) balance_of_wallet_B_2 = int(API.rpc().getbalance(wallet_B_address)[1]["result"]["ont"]) # step 4 compare self.ASSERT(balance_of_wallet_B_1 == balance_of_wallet_B_2, "balance of wallte B changed.") self.ASSERT(balance_of_wallet_A_1 - balance_of_wallet_A_2 == int(vote_price), "the decrease of balance of wallet A is not %s." % vote_price) except Exception as e: logger.print(e.args[0])
def test_normal_088_getsmartcodeeventbyhash(self): try: (process, response) = API.native().transfer_ont(Config.NODES[0]["address"], Config.NODES[1]["address"], "100000000", 0, twice=True, check_state=True) tx_hash_failed = response["txhash"] (process, response) = API.ws().getsmartcodeeventbyhash(tx_hash_failed) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_normal_100_getmerkleproof(self): try: (process, response) = API.native().transfer_ont(Config.NODES[0]["address"], Config.NODES[1]["address"], "100000000", 0, twice=True, check_state=False) tx_hash_failed = response["txhash"] API.node().wait_tx_result(tx_hash_failed) (process, response) = API.ws().getmerkleproof(tx_hash_failed) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_base_001_consensus(self): process = False try: (process, response) = test_api.transfer( test_config.m_contract_addr, Config.NODES[test_config.m_current_node]["address"], Config.NODES[1]["address"], test_config.AMOUNT, test_config.m_current_node) self.ASSERT(process, "transfer error...") API.node().wait_gen_block() (process, response) = API.rpc().getblockheightbytxhash(response["txhash"]) self.ASSERT(process, "not a valid block...") except Exception as e: logger.print(e.args[0])
def regIDWithPublicKey(ontId, public_key, node_index=None, errorcode=47001): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "method": "regIDWithPublicKey", "address": "0300000000000000000000000000000000000000", "version": 1, "params": [ontId, public_key] } }, "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 return API.contract().call_contract(Task(name="regIDWithPublicKey", ijson=request), twice=True)
def test_abnormal_092_getsmartcodeeventbyhash(self): try: (process, response) = API.ws().getsmartcodeeventbyhash( test_config.TX_HASH_INCORRECT_4) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_093_getblockheightbytxhash(self): try: (process, response) = API.ws().getblockheightbytxhash( test_config.TX_HASH_CORRECT) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_normal_027_getblockhashbyheight(self,height=65536): try: (process, response) = API.restful().getblockhashbyheight(height) rs = (response["Result"] == "" or response["Result"] == None) self.ASSERT(not process and rs, "") except Exception as e: logger.print(e.args[0])
def invoke_function_opCode(contract_address, function_str, param_1, param_2, sleep=0): 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": "int", "value": param_1 }, { "type": "int", "value": param_2 } ] } ] } }, "RESPONSE":{"error" : 0} } return API.contract().call_contract(Task(name="invoke_function_opCode", ijson=request), twice = True, sleep=sleep)
def test_abnormal_098_getblockheightbytxhash(self): try: (process, response) = API.ws().getblockheightbytxhash( test_config.TX_HASH_INCORRECT_4) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_099_getmerkleproof(self): try: (process, response) = API.ws().getmerkleproof(test_config.TX_HASH_CORRECT) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def getDDO(ontId, public_key, node_index=None, errorcode=47001, errorkey="error"): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "method": "getDDO", "address": "0300000000000000000000000000000000000000", "version": 1, "params": [ontId] } }, "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 return API.contract().call_contract(Task(name="getDDO", ijson=request), twice=True, sleep=2)
def transfer(): request = { "REQUEST": { "Qid": "t", "Method": "signativeinvoketx", "Params": { "gas_price": 0, "gas_limit": 1000000000, "method": "transfer", "address": "0100000000000000000000000000000000000000", "version": 1, "params": [[[ "AZKG25z2yjCUTQZtNYb9Pq4dezP4zGRFsP", "AS9S7j6VWUgg3sX89pok6KHeYU9DenKwbS", "100" ]]] } }, "RESPONSE": { "error": 0 } } return API.contract( ).call_multisig_contract(Task(name="transfer", ijson=request), 3, [ "031b7025752886d5598e3a165a7ba4accc770955e4450ee5e14d38d35c7dfef8a5", "03b36e26db8b132b551a0213ade4eeb39510847acd152490ef170604c5e4e4686d", "03f8d89ab17423d9e4a57b7e7174bcf239eb42779fd942b0601b778087440d35aa" ])
def test_base_087_getsmartcodeeventbyhash(self): try: (process, response) = API.ws().getsmartcodeeventbyhash( test_config.TX_HASH_CORRECT) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def addKey(ontId, new_public_key, public_key, node_index=None, errorcode=47001, public_key_Array=[]): 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": { "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(public_key_Array) == 0 or len(public_key_Array) > 2: return API.contract().call_contract(Task(name="addKey", ijson=request), twice=True) else: return multi_contract(Task(name="addKey", ijson=request), public_key_Array[0], public_key_Array[1])
def test_base_004_subscribe(self): try: (process, response) = API.ws().subscribe( [test_config.CONTRACT_ADDRESS_CORRECT]) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_104_getmerkleproof(self): try: (process, response) = API.ws().getmerkleproof( test_config.TX_HASH_INCORRECT_4) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])