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 nodeCountCheck(InResponse, nodeCount): (result1, response1) = API.rpc().getstorage( "0700000000000000000000000000000000000000", ByteToHex(b"governanceView")) if not result1: return (False, {"error_info": "getstorage governanceView error !"}) viewvalue = "" viewvalue = response1["result"][0:8] print(viewvalue) (result1, response1) = API.rpc().getstorage( "0700000000000000000000000000000000000000", ByteToHex(b"peerPool") + viewvalue) if not result1: return (False, { "error_info": "getstorage peerPool error ! viewValue:" + viewvalue }) resCheck = Common.bl_reserver(response1["result"][0:2]) print(resCheck) resInt = bytes.fromhex(resCheck) print(resInt.hex()) if isinstance(InResponse, dict): InResponse["nodeCountNow"] = int(resInt.hex(), 16) else: InResponse = { "response": InResponse, "nodeCountNow": int(resInt.hex(), 16) } if (nodeCount == int(resInt.hex(), 16)): return (True, InResponse) else: return (False, InResponse)
def test_init(self): 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) while True: if API.rpc().getblockcount()[1]["result"] > 0: break time.sleep(1) (test_config.m_contractaddr_right, test_config.m_txhash_right) = API.contract().deploy_contract_full( testpath + "/resource/A.neo", "name", "desc", 0) time.sleep(5) (result, reponse) = API.rpc().getblockhash(height=1) test_config.m_block_hash_right = reponse["result"] (result, reponse) = API.contract().sign_transction( Task(testpath + "/resource/cli/siginvoketx.json"), False) test_config.m_signed_txhash_right = reponse["result"]["signed_tx"] test_config.m_signed_txhash_wrong = "0f0f0f0f" + test_config.m_signed_txhash_right API.contract().invoke_function( test_config.m_contractaddr_right, "put", "", "1", argvs=[{ "type": "bytearray", "value": test_config.m_getstorage_contract_key }, { "type": "bytearray", "value": test_config.m_getstorage_contract_value }], node_index=0)
def test_abnormal_003_benefit(self): try: address = Config.NODES[self.m_checknode]["address"] # logger.open("test_003_benefit.log", "test_003_benefit") process = True (process, response) = API.rpc().getbalance(address) self.BLOCK(process, "get balance error[1]") ong1 = int(response["result"]["ong"]) ont1 = int(response["result"]["ont"]) API.node().transfer_ont(0, 0, 9999999999999999999, test_config.PRICE_TEST) #判断是否分润,至少需要等待1个共识时间 (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() (process, response) = API.rpc().getbalance(address) self.BLOCK(process, "get balance error[2]") ong2 = int(response["result"]["ong"]) ont2 = int(response["result"]["ont"]) self.ASSERT((ong2 - ong1) == 0, "error") except Exception as e: logger.print(e.args[0]) process = False
def test_normal_007_benefit(self): try: process = False address1 = Config.NODES[self.m_checknode]["address"] (process, response) = API.rpc().getbalance(address1) self.BLOCK(process, "get balance error[1]") ong1 = int(response["result"]["ong"]) (process, response) = API.native().transfer_ont( Config.NODES[0]["address"], Config.NODES[0]["address"], "1", gas_price=test_config.PRICE_TEST) #判断是否分润,至少需要等待1个共识时间 except_benifit = int( test_api.get_benifit_value( 20000 * test_config.PRICE_TEST * 0.5, 1000, [1000, 1000, 1000, 1000, 1000, 1000, 1000])) logger.print("except_benifit: " + str(except_benifit)) (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() (process, response) = API.rpc().getbalance(address1) self.BLOCK(process, "get balance error[2]") ong2 = int(response["result"]["ong"]) print("before cost[1]: " + str(ong1)) print("after cost[1]: " + str(ong2)) self.ASSERT((int(ong2 - ong1) == int(except_benifit)), "") except Exception as e: logger.print(e.args[0])
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_002_benefit(self): process = False try: address1 = Config.NODES[self.m_checknode]["address"] (process, response) = API.rpc().getbalance(address1) self.BLOCK(process, "get balance error") ong1 = int(response["result"]["ong"]) (process, response) = API.native().transfer_ont( Config.NODES[0]["address"], Config.NODES[0]["address"], "1", gas_price=test_config.PRICE_TEST) #判断是否分润,至少需要等待1个共识时间 (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() (process, response) = API.rpc().getbalance(address1) self.BLOCK(process, "get balance error") ong2 = int(response["result"]["ong"]) print("before cost[1]: " + str(ong1)) print("after cost[1]: " + str(ong2)) self.ASSERT(ong2 != ong1, "get balance error") except Exception as e: logger.print(e.args[0]) process = False
def test_normal_013_benefit(self): try: address = Config.NODES[self.m_checknode]["address"] process = False unpeer_node = 10 #未被投票节点 API.native().update_global_param("0", "1000", "32", "10", "0", "100", "5", "5") for i in range(7, 14): test_api.add_candidate_node(i, init_pos=5000, from_node=i - 7) #先共识一次,确保节点都会在下一次共识分红 (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() #交易 (process, response) = API.native().transfer_ont( Config.NODES[0]["address"], Config.NODES[0]["address"], "1", gas_price=test_config.PRICE_TEST) (process, response) = API.rpc().getbalance(address) self.BLOCK(process, "get balance error") normal_ong = int(response["result"]["ong"]) (process, response) = API.rpc().getbalance( Config.NODES[unpeer_node]["address"]) self.BLOCK(process, "get balance error") candidate_ong = int(response["result"]["ong"]) (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() (process, response) = API.rpc().getbalance(address) self.BLOCK(process, "get balance error") normal_ong2 = int(response["result"]["ong"]) (process, response) = API.rpc().getbalance( Config.NODES[unpeer_node]["address"]) self.BLOCK(process, "get balance error") candidate_ong2 = int(response["result"]["ong"]) except_benifit1 = int( test_api.get_candidate_benifit_value( 20000 * test_config.PRICE_TEST, 5000, [5000, 5000, 5000, 5000, 5000, 5000, 5000])) except_benifit2 = 0 logger.info("normal_ong1: " + str(normal_ong)) logger.info("normal_ong2: " + str(normal_ong2)) logger.info("candidate_ong1: " + str(candidate_ong)) logger.info("candidate_ong2: " + str(candidate_ong2)) self.ASSERT((normal_ong2 - normal_ong) == except_benifit2, "benefit normal node error") self.ASSERT((candidate_ong2 - candidate_ong) == except_benifit1, "benefit candidate node error") except Exception as e: logger.print(e.args[0]) process = False
def test_normal_005_benefit(self): try: process = False #启动拜占庭节点 API.node().stop_nodes(self.m_dbft_nodes) API.node().start_nodes(self.m_dbft_nodes, Config.DEFAULT_NODE_ARGS, True, True, program="ontology-bft_1") (process, response) = API.native().update_global_param( "0", "1000", "32", "1", "50", "50", "5", "5") self.BLOCK(process, "updateGlobalParam error") (process, response) = API.rpc().getbalance( Config.NODES[self.m_checknode]["address"]) self.BLOCK(process, "get balance error") ong1 = int(response["result"]["ong"]) (process, response) = API.rpc().getbalance( Config.NODES[self.m_dbft_nodes[0]]["address"]) self.BLOCK(process, "get balance error") dbft_ong1 = int(response["result"]["ong"]) #进行第一轮共识 (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() (process, response) = API.native().transfer_ont( Config.NODES[0]["address"], Config.NODES[0]["address"], "1", gas_price=test_config.PRICE_TEST) (process, response) = API.native().commit_dpos(sleep=0) self.BLOCK(process, "commit_dpos error") API.node().wait_gen_block() (process, response) = API.rpc().getbalance( Config.NODES[self.m_checknode]["address"]) self.BLOCK(process, "get balance error") ong2 = int(response["result"]["ong"]) (process, response) = API.rpc().getbalance( Config.NODES[self.m_dbft_nodes[0]]["address"]) self.BLOCK(process, "get balance error") dbft_ong2 = int(response["result"]["ong"]) #第二轮判断 except_benifit = int( test_api.get_benifit_value( 20000 * test_config.PRICE_TEST * 0.5, 10000, [10000, 10000, 10000, 10000, 10000, 10000, 10000])) self.ASSERT((dbft_ong2 != dbft_ong1), "bft node benefit error") self.ASSERT((ong2 != ong1), "normal node benefit error") 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_abnormal_090_getmerkleproof(self): try: (process, response) = API.rpc().getmerkleproof(test_config.m_txhash_wrong) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_089_getmerkleproof(self): try: (process, response) = API.rpc().getmerkleproof(test_config.m_txhash_right) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_086_getbalance(self): try: (process, response) = API.rpc().getbalance( test_config.getbalance_address_false) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_085_getbalance(self): try: (process, response) = API.rpc().getbalance( test_config.getbalance_address_true) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_084_getblockheightbytxhash(self): try: (process, response) = API.rpc().getblockheightbytxhash(tx_hash=None) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_080_getblockheightbytxhash(self): try: (process, response) = API.rpc().getblockheightbytxhash( tx_hash=test_config.m_txhash_right) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_normal_037_getrawtransaction(self): try: (process, response) = API.rpc().getrawtransaction( test_config.m_txhash_right, None) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_016_getblockhash(self): try: (process, response) = API.rpc().getblockhash( height=test_config.m_block_height_overflow) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_normal_068_getcontractstate(self): try: (process, response) = API.rpc().getcontractstate( test_config.m_contractaddr_right, None) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_normal_023_getblockcount(self): try: # self.clear_nodes() (process, response) = API.rpc().getblockcount() self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_059_getcontractstate(self): try: (process, response) = API.rpc().getcontractstate(test_config.m_txhash_wrong) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_050_getstorage(self): try: (process, response) = API.rpc().getstorage( test_config.m_getstorage_contract_addr, None) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_038_sendrawtransaction(self): try: (process, response) = API.rpc().sendrawtransaction( test_config.m_signed_txhash_right) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_abnormal_070_getmempooltxstate(self): try: (process, response) = API.rpc().getmempooltxstate( test_config.m_txhash_right) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_013_getblockhash(self): try: (process, response) = API.rpc().getblockhash( height=test_config.m_block_height_right) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_normal_010_consensus(self): try: process = False storage_key = ByteToHex(b'Test Key 10') storage_value = ByteToHex(b'Test Value 10') for i in range(1, 4): self.init_bft_node(i) API.node().wait_gen_block() time.sleep(5) (process, response) = API.contract().invoke_function( test_config.m_contract_addr, "put", "", "1", argvs=[{ "type": "bytearray", "value": storage_key }, { "type": "bytearray", "value": storage_value }], node_index=test_config.m_current_node, sleep=0) API.node().wait_gen_block() time.sleep(5) self.ASSERT(process, "invoke_function error...") (process, response) = API.rpc().getblockheightbytxhash( response["txhash"]) self.ASSERT(process, "not a valid block...") except Exception as e: logger.print(e.args[0])
def test_abnormal_079_getsmartcodeevent(self): try: (process, response) = API.rpc().getsmartcodeevent( tx_hash=test_config.m_txhash_wrong) self.ASSERT(not process, "") except Exception as e: logger.print(e.args[0])
def test_base_074_getsmartcodeevent(self): try: (process, response) = API.rpc().getsmartcodeevent( height=test_config.getsmartcodeevent_height) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])
def test_02_getaccountstate(self): try: result = API.rpc().getaccountstate( Config.NODES[0]["wallet"].account().address()) self.ASSERT(result, "") except Exception as e: logger.error(traceback.format_exc())
def test_base_041_getstorage(self): try: (process, response) = API.rpc().getstorage( test_config.m_getstorage_contract_addr, test_config.m_getstorage_contract_key) self.ASSERT(process, "") except Exception as e: logger.print(e.args[0])