def test_base_047_getblockheight(self): try: (process, response) = API.ws().getblockheight() self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_51_get_balance(self, attr=""): logger.open("restful/51_get_balance.log", "51_get_balance") (result, response) = RestfulApi().getbalance(attr) logger.close(not result)
def test_58_get_smtcode_evt_txs(self, height=""): logger.open("restful/58_get_smtcode_evt_txs.log", "58_get_smtcode_evt_txs") (result, response) = RestfulApi().getsmartcodeeventbyheight(height) logger.close(not result)
def test_31_get_tx(self): logger.open("restful/31_get_tx.log", "31_get_tx") (result, response) = RestfulApi().gettransactionbytxhash(self.m_txhash_right) logger.close(result)
def test_49_get_balance(self): attr = self.getbalance_address_true logger.open("restful/49_get_balance.log", "49_get_balance") (result, response) = RestfulApi().getbalance(attr) logger.close(result)
def test_22_get_blk_height(self): logger.open("restful/22_get_blk_height.log", "22_get_blk_height") (result, response) = RestfulApi().getblockheight() logger.close(result)
def test_27_get_blk_hash(self, height=65536): logger.open("restful/27_get_blk_hash.log", "27_get_blk_hash") (result, response) = RestfulApi().getblockhashbyheight(height) logger.close(result and (response["Result"] == "" or response["Result"] == None))
def call_contract(self, task, judge=True, pre=True, twice=False, sleep=5, check_state=True): try: logger.print("\n") logger.print("[-------------------------------]") logger.print("[ RUN ] " + "contract" + "." + task.name()) taskdata = task.data() node_index = None deploy_first = False deploy_code_path = None deploy_contract_addr = None for key in taskdata: if key.upper() == "DEPLOY": deploy_first = taskdata[key] if key.upper() == "CODE_PATH": deploy_code_path = taskdata[key] if key.upper() == "NODE_INDEX": node_index = int(taskdata[key]) if deploy_first: deploy_contract_addr = self.deploy_contract(deploy_code_path) #step 1: signed tx expect_response = None expect_signresponse = None if "RESPONSE" in taskdata: expect_response = taskdata["RESPONSE"] if "SIGN_RESPONSE" in taskdata: expect_signresponse = taskdata["SIGN_RESPONSE"] if deploy_contract_addr: taskdata["REQUEST"]["Params"][ "address"] = deploy_contract_addr.strip() (result, response) = self.sign_transction(task, True, False) task.data()["RESPONSE"] = response logger.print("[ SIGNED TX ] " + json.dumps(taskdata, indent=4)) #step 2: call contract if expect_signresponse != None: result = Common.cmp(expect_signresponse, response) if result and "error_code" in response and int( response["error_code"]) != 0: return (result, response) if not result: raise Error("not except sign result") signed_tx = None if not response is None and "result" in response and not response[ "result"] is None and "signed_tx" in response["result"]: signed_tx = response["result"]["signed_tx"] if signed_tx == None or signed_tx == '': raise Error("no signed tx") if twice: (result, response) = self.call_signed_contract(signed_tx, True, node_index) (result2, response2) = self.call_signed_contract( signed_tx, False, node_index) if response and response2 and "result" in response2: response["txhash"] = response2["result"] else: (result, response) = self.call_signed_contract(signed_tx, pre, node_index) if not pre: response["txhash"] = response["result"] if response is None or "error" not in response: # or str(response["error"]) != '0': raise Error("call contract error") response["signed_tx"] = signed_tx if deploy_contract_addr: response["address"] = taskdata["REQUEST"]["Params"]["address"] #判断交易state是否成功,代替等待区块 if check_state and response and ("txhash" in response) and ( twice or pre == False): result2 = nodeapi.wait_tx_result(response["txhash"]) if twice: result3 = False if response["error"] == 0: result3 = result2 else: result3 = not result2 if not result3: raise Error("tx state not match") if judge and expect_response: result = Common.cmp(expect_response, response) #if not result: # raise Error("not except result") #time.sleep(sleep) return (result, response) except Error as err: return (False, {"error_info": err.msg})
def call_multisig_contract(self, task, m, pubkeyArray, sleep=5, check_state=True): try: taskdata = task.data() expect_signresponse = None expect_response = None if "RESPONSE" in taskdata: expect_response = taskdata["RESPONSE"] if "SIGN_RESPONSE" in taskdata: expect_signresponse = taskdata["SIGN_RESPONSE"] (result, response) = self.sign_transction( task) #Task(name="multi", ijson=request)) if not result: logger.error("call_multisig_contract.sign_transction error!") return (result, response) if expect_signresponse != None: result = Common.cmp(expect_signresponse, response) if result and "error_code" in response and int( response["error_code"]) != 0: return (result, response) if not result: raise Error("not except sign result") signed_tx = response["result"]["signed_tx"] #print(request1) execNum = 0 signed_raw = signed_tx for pubkey in pubkeyArray: request1 = { "REQUEST": { "qid": "1", "method": "sigmutilrawtx", "params": { "raw_tx": signed_raw, "m": m, "pub_keys": pubkeyArray } }, "RESPONSE": { "error_code": 0 } } for node_index in range(len(Config.NODES)): if Config.NODES[node_index]["pubkey"] == pubkey: request1["NODE_INDEX"] = node_index (result, response) = self.sign_multi_transction( Task(name="multi", ijson=request1)) if not result: logger.error( "call_multisig_contract.sign_multi_transction error![1]" ) return (result, response) if "error_code" not in response or response[ "error_code"] != 0: logger.error( "call_multisig_contract.sign_multi_transction error![2]" ) return (False, response) signed_raw = response["result"]["signed_tx"] logger.info("multi sign tx:" + str(execNum) + pubkey) execNum = execNum + 1 break if execNum >= m: (result, response) = self.call_signed_contract(signed_raw, True) (result2, response2) = self.call_signed_contract(signed_raw, False) #判断交易state是否成功,代替等待区块 if response and response2 and "result" in response2: response["txhash"] = response2["result"] if check_state and response and ("txhash" in response): result2 = nodeapi.wait_tx_result(response["txhash"]) result3 = False if response["error"] == 0: result3 = result2 else: result3 = not result2 if not result3: raise Error("tx state not match") if expect_response: result = Common.cmp(expect_response, response) #if not result: # raise Error("not except result") time.sleep(sleep) return (result, response) return (False, { "error_info": "multi times lesss than except!only " + str(execNum) }) except Exception as e: return (False, {"error_info": e})
def setUp(self): API.clirpc().init(self._testMethodName, Config.NODES[0]["path"]) logger.open("test_cli/" + self._testMethodName + ".log", self._testMethodName)
def tearDown(self): logger.close(self.result())
def setUp(self): logger.open("test_websocket/" + self._testMethodName + ".log", self._testMethodName) if self._testMethodName == "test_init": return
def test_base_105_getsessioncount(self): try: (process, response) = API.ws().getsessioncount() self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_base_001_heartbeat(self): try: (process, response) = API.ws().heartbeat() self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_20_get_blk_by_hash(self): logger.open("restful/20_get_blk_by_hash.log", "20_get_blk_by_hash") (result, response) = RestfulApi().getblockbyhash(self.m_block_hash_right, 1) logger.close(result)
def get_benifit_value(totalgas, initpos, totalpos=[1000, 1000, 1000, 1000, 1000, 1000, 1000]): totalyi = 0 totalposvalue = 0 for pos in totalpos: totalposvalue = totalposvalue + pos avgpos = int(totalposvalue / len(totalpos)) print("avgpos: " + str(avgpos)) for pos in totalpos: totalyi = totalyi + test_api.get_yi(pos, avgpos) if totalyi == 0: return 0 logger.info("get_benifit_value.totalgas: " + str(totalgas)) logger.info("get_benifit_value.initpos: " + str(initpos)) logger.info("get_benifit_value.avgpos: " + str(avgpos)) logger.info("get_benifit_value.totalyi: " + str(totalyi)) logger.info("get_benifit_value.get_yi: " + str(test_api.get_yi(initpos, avgpos))) logger.info("get_benifit_value: " + str(totalgas * test_api.get_yi(initpos, avgpos) / totalyi)) return totalgas * test_api.get_yi(initpos, avgpos) / totalyi
def test_21_get_blk_by_hash(self): logger.open("restful/21_get_blk_by_hash.log", "21_get_blk_by_hash") (result, response) = RestfulApi().getblockbyhash(self.m_block_hash_error, 1) logger.close(not result)
def test_main(self): result = False logger.open("TestGover14.log", "TestGover14") try: if (not pause( "ensure that node A and node B is in the nodes network and node A has more than 10000 ont." )): raise Error("pre-condition unsatisfied") (wallet_A_address, wallet_B_address, vote_price, node_B_puiblic_key, blocks_per_round, punish_ratio) = get_config() consensus_rounds = (int)(getblockcount() / blocks_per_round) ''' # to ensure that the following operations are in the same round while(True): if getblockcount() % blocks_per_round <= 2: time.sleep(1) continue else: break ''' ''' # step 1 wallet A vote for node B (result, response) = vote_for_peer(wallet_A_address, ["03e05d01e5df2c85e6a9a5526c70d080b6c7dce0fa7c66f8489c18b8569dc269dc"], ["190000"]) print (response) #if not result: # raise Error("vote_for_peer error") # step 1 wallet A vote for node B (result, response) = vote_for_peer(wallet_A_address, ["03e05d01e5df2c85e6a9a5526c70d080b6c7dce0fa7c66f8489c18b8569dc269dc"], ["10000"]) print (response) #if not result: # raise Error("vote_for_peer error") ''' (result, response) = vote_for_peer(wallet_A_address, [node_B_puiblic_key], ["90000"]) (result, response) = unvote_for_peer(wallet_A_address, [node_B_puiblic_key], ["10000"]) while (True): if (getblockcount() / blocks_per_round - consensus_rounds == 1): (result, response) = vote_for_peer(wallet_A_address, [node_B_puiblic_key], ["10001"]) break else: print(getblockcount()) time.sleep(5) print("waiting...") continue (result, response) = withdraw_ont(wallet_A_address, [node_B_puiblic_key], ["10000"]) except Exception as e: print(e.msg) logger.close(result)
def test_24_get_blk_hash(self, height=1): logger.open("restful/24_get_blk_hash.log", "24_get_blk_hash") (result, response) = RestfulApi().getblockhashbyheight(height) logger.close(result)
def test_main(self): result = False logger.open("TestGover4.log", "TestGover4") try: if (not pause( "ensure that node A is not in the nodes network and node B is in the nodes network and node A has more than 10000 ont." )): raise Error("pre-condition unsatisfied") (wallet_A_address, wallet_B_address, vote_price, node_B_puiblic_key, blocks_per_round, punish_ratio) = get_config() consensus_rounds = (int)(getblockcount() / blocks_per_round) # step 1 wallet A vote for node B (result, response) = vote_for_peer(wallet_A_address, [node_B_puiblic_key], [vote_price]) print(response) #if not result: # raise Error("vote_for_peer error") # step 2 wallet A unvote in the second round while (True): if (getblockcount() / blocks_per_round - consensus_rounds == 1): (result, response) = unvote_for_peer(wallet_A_address, [node_B_puiblic_key], [vote_price]) break else: print(getblockcount()) time.sleep(5) continue #if not result: # raise Error("unvote_for_peer error") # step 3 wallet A withdraw ont in the third round while (True): if (getblockcount() / blocks_per_round - consensus_rounds == 2): # should fail (result, response) = withdraw_ont(wallet_A_address, [node_B_puiblic_key], [vote_price]) break else: print(getblockcount()) time.sleep(5) continue #if not result: # raise Error("withdraw_ont error") # step 4 wallet A withdraw ont in the forth round while (True): if (getblockcount() / blocks_per_round - consensus_rounds == 3): (result, response) = withdraw_ont(wallet_A_address, [node_B_puiblic_key], [vote_price]) break else: print(getblockcount()) time.sleep(5) continue #if not result: # raise Error("withdraw_ont error") # this should be failed (result, response) = withdraw_ont(wallet_A_address, [node_B_puiblic_key], ["1"]) #if not result: # raise Error("withdraw_ont error") except Exception as e: print(e.msg) logger.close(result)
def test_30_get_blk_hash(self, height=""): logger.open("restful/30_get_blk_hash.log", "30_get_blk_hash") (result, response) = RestfulApi().getblockhashbyheight(height) logger.close(not result)
def test_02_get_gen_blk_time(self): logger.open("restful/02_get_gen_blk_time.log", "02_get_gen_blk_time") (result, response) = RestfulApi().getgenerateblocktime() logger.close(result)
def test_32_get_tx(self): logger.open("restful/32_get_tx.log", "32_get_tx") (result, response) = RestfulApi().gettransactionbytxhash(self.m_txhash_wrong) logger.close(not result)
def test_03_get_conn_count(self): logger.open("restful/03_get_conn_count.log", "03_get_conn_count") (result, response) = RestfulApi().getconnectioncount() logger.close(result)
def test_50_get_balance(self): attr = self.getbalance_address_false logger.open("restful/50_get_balance.log", "50_get_balance") (result, response) = RestfulApi().getbalance(attr) logger.close(not result)
def test_10_get_blk_txs_by_height(self, height=-1): logger.open("restful/10_get_blk_txs_by_height.log", "10_get_blk_txs_by_height") (result, response) = RestfulApi().getblocktxsbyheight(height) logger.close(not result)
def test_06_get_blk_txs_by_height(self, height=0): logger.open("restful/06_get_blk_txs_by_height.log", "06_get_blk_txs_by_height") (result, response) = RestfulApi().getblocktxsbyheight(height) logger.close(result)
def test_19_get_blk_by_height(self, height=""): logger.open("restful/19_get_blk_by_height.log", "19_get_blk_by_height") (result, response) = RestfulApi().getblockbyheight(height) logger.close(not result)
def test_59_get_smtcode_evts(self): hash = self.m_txhash_right logger.open("restful/59_get_smtcode_evts.log", "59_get_smtcode_evts") (result, response) = RestfulApi().getsmartcodeeventbyhash(hash) logger.close(result)
def test_normal_028_getconnectioncount(self): try: (process, response) = API.ws().getconnectioncount({"height": "1"}) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])