def test_new_account_transaction(): ''' 新建账号存在余额后转账给其他账号 ''' nocollusion_w3 = connect_web3(nocollusion_list[0]["url"]) eth = Eth(nocollusion_w3) # 先给新账户存钱 new_account = nocollusion_w3.toChecksumAddress( nocollusion_w3.personal.newAccount(conf.PASSWORD)) from_account = nocollusion_w3.toChecksumAddress(conf.ADDRESS) nocollusion_w3.personal.unlockAccount(from_account, conf.PASSWORD, 666666) trans_hex = transaction(nocollusion_w3, from_account, new_account) eth.waitForTransactionReceipt(HexBytes(trans_hex).hex()) # 新账户把钱转给其他账户 new_account2 = nocollusion_w3.toChecksumAddress( nocollusion_w3.personal.newAccount(conf.PASSWORD)) before_value = eth.getBalance(new_account2) nocollusion_w3.personal.unlockAccount(new_account, conf.PASSWORD, 666666) trans_hex2 = transaction(nocollusion_w3, new_account, new_account2, value=100) eth.waitForTransactionReceipt(HexBytes(trans_hex2).hex()) after_value = eth.getBalance(new_account2) assert after_value - before_value == 100, '交易失败,转账金额未到账'
def test_platon_GetBalance(global_running_env): node = global_running_env.get_rand_node() platon = Eth(node.web3) account = global_running_env.account addr = account.account_with_money["address"] from_addr = Web3.toChecksumAddress(addr) balance = platon.getBalance(from_addr) print("\n当前账户【{}】的余额为:{}".format(from_addr, balance)) balance = platon.getBalance("0x1111111111111111111111111111111111111111") assert balance == 0 print("\n当前不存在的账户【{}】余额为:{}".format( "0x1111111111111111111111111111111111111111", balance))
def test_platon_GetBalance(global_running_env): node = global_running_env.get_rand_node() platon = Eth(node.web3) account = global_running_env.account addr = account.account_with_money["address"] from_addr = Web3.toChecksumAddress(addr) # balance = platon.getBalance(from_addr) balance = platon.getBalance(node.web3.pipAddress) assert balance == 0
def test_consensus_unlock_sendtransaction(): ''' 共识节点unlock转账 ''' collusion_w3 = connect_web3(collusion_list[0]["url"]) nocollusion_w3 = connect_web3(nocollusion_list[0]["url"]) eth = Eth(collusion_w3) from_account = collusion_w3.toChecksumAddress(conf.ADDRESS) # 非共识节点新建一个钱包 new_account = nocollusion_w3.personal.newAccount(conf.PASSWORD) time.sleep(2) to_account = collusion_w3.toChecksumAddress(new_account) before_value = eth.getBalance(to_account) collusion_w3.personal.unlockAccount(from_account, conf.PASSWORD, 666666) # 共识节点转账给非共识节点新建的钱包 tx_hash = transaction(collusion_w3, from_account, to_account) transaction_hash = HexBytes(tx_hash).hex() try: result = eth.waitForTransactionReceipt(transaction_hash) except: assert False, '等待超时,交易哈希:{}'.format(transaction_hash) after_value = eth.getBalance(to_account) assert result is not None, '交易在pending,未上链' assert after_value - before_value == 1000000000000000000000, '交易失败,转账金额未到账'
def batch_send_transfer(accountsfile, config): # 交易文件列表 if accountsfile == "": print("-f/--accountsfile is null, please input.") exit(1) # 发送交易的节点配置信息 if config == "": print("-c/--config is null, please input.") exit(1) try: # 获取节点 url with open(config, 'r') as load_f: config_info = json.load(load_f) url = config_info['nodeAddress'] + ":" + config_info['nodeRpcPort'] w3 = Web3(HTTPProvider(url)) platon = Eth(w3) # 获取交易列表 rows = read_csv(accountsfile) for row in rows: if 'address' in row: address = row["address"] else: raise Exception( "The address field does not exist in the address file!") amount = platon.getBalance(Web3.toChecksumAddress(address)) amount = w3.fromWei(amount, "ether") print("address:{}, amount:{}LAT".format(address, amount)) except Exception as e: print('{} {}'.format('exception: ', e)) print('batch get account balance failure!!!') sys.exit(1) else: print('batch get account balance SUCCESS.') end = datetime.datetime.now() print("end:{}".format(end)) print("总共消耗时间:{}s".format((end - start).seconds)) sys.exit(0)
def test_wallet_not_on_chain(): '''' 转账给不在一条链上的钱包,能转账成功 ''' collusion_w3 = connect_web3(collusion_list[0]["url"]) eth = Eth(collusion_w3) from_account = collusion_w3.toChecksumAddress(conf.ADDRESS) to_account = collusion_w3.toChecksumAddress( '0x2fd96b410e4472a687fc1a872ef70abbb658bb9a') before_value = collusion_w3.eth.getBalance(to_account) collusion_w3.personal.unlockAccount(from_account, conf.PASSWORD, 666666) tx_hash = transaction(collusion_w3, from_account, to_account) transaction_hash = HexBytes(tx_hash).hex() try: result = eth.waitForTransactionReceipt(transaction_hash) except: assert False, '等待超时,交易哈希:{}'.format(transaction_hash) after_value = eth.getBalance(to_account) assert result is not None, '交易在pending,未上链' assert after_value - before_value == 1000000000000000000000, '交易失败,转账金额未到账'
class TestUndelegate(): node_yml_path = conf.NODE_YML node_info = get_node_info(node_yml_path) rpc_list, enode_list, nodeid_list, ip_list, port_list = node_info.get( 'collusion') rpc_list2, enode_list2, nodeid_list2, ip_list2, port_list2 = node_info.get( 'nocollusion') address = Web3.toChecksumAddress(conf.ADDRESS) privatekey = conf.PRIVATE_KEY account_list = conf.account_list privatekey_list = conf.privatekey_list externalId = "1111111111" nodeName = "platon" website = "https://www.test.network" details = "supper node" programVersion = 1792 illegal_nodeID = conf.illegal_nodeID genesis_path = conf.GENESIS_TMP """替换config.json""" get_config_data() config_json_path = conf.PLATON_CONFIG_PATH config_dict = LoadFile(config_json_path).get_data() amount_delegate = Web3.fromWei( int(config_dict['EconomicModel']['Staking']['MinimumThreshold']), 'ether') amount = Web3.fromWei( int(config_dict['EconomicModel']['Staking']['StakeThreshold']), 'ether') def setup_class(self): self.auto = AutoDeployPlaton() self.auto.start_all_node(self.node_yml_path) self.genesis_dict = LoadFile(self.genesis_path).get_data() self.chainid = int(self.genesis_dict["config"]["chainId"]) self.ppos_link = Ppos(self.rpc_list[0], self.address, self.chainid) self.w3_list = [connect_web3(url) for url in self.rpc_list] """用新的钱包地址和未质押过的节点id封装对象""" self.ppos_noconsensus_1 = Ppos(self.rpc_list[0], self.account_list[0], self.chainid, privatekey=self.privatekey_list[0]) self.ppos_noconsensus_2 = Ppos(self.rpc_list[0], self.account_list[1], self.chainid, privatekey=self.privatekey_list[1]) self.ppos_noconsensus_3 = Ppos(self.rpc_list[0], self.account_list[2], self.chainid, privatekey=self.privatekey_list[2]) self.ppos_noconsensus_4 = Ppos(self.rpc_list[0], self.account_list[3], self.chainid, privatekey=self.privatekey_list[3]) self.ppos_noconsensus_5 = Ppos(self.rpc_list[0], self.account_list[4], self.chainid, privatekey=self.privatekey_list[4]) self.ppos_noconsensus_6 = Ppos(self.rpc_list[0], self.account_list[5], self.chainid, privatekey=self.privatekey_list[5]) self.eth = Eth(self.w3_list[0]) def transaction(self, w3, from_address, to_address=None, value=1000000000000000000000000000000000, gas=91000000, gasPrice=9000000000, pwd=conf.PASSWORD): """" 转账公共方法 """ personal = Personal(w3) personal.unlockAccount(from_address, pwd, 666666) params = { 'to': to_address, 'from': from_address, 'gas': gas, 'gasPrice': gasPrice, 'value': value } tx_hash = w3.eth.sendTransaction(params) result = w3.eth.waitForTransactionReceipt(HexBytes(tx_hash).hex()) return result def getCandidateList(self): """ 获取实时验证人的nodeID list """ msg = self.ppos_noconsensus_1.getCandidateList() recive_list = msg.get("Data") nodeid_list = [] if recive_list is None: return recive_list else: for node_info in recive_list: nodeid_list.append(node_info.get("NodeId")) return nodeid_list ##############################赎回#################################################### @allure.title("委托人未达到锁定期申请赎回") @pytest.mark.P0 def test_unDelegate_part(self): """ 用例id 106 申请赎回委托 用例id 111 委托人未达到锁定期申请赎回 申请部分赎回 """ log.info("转账到钱包3") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[2]) log.info("转账到钱包4") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[3]) msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[2]) if msg['Data'] == "": log.info("质押节点3:{}成为验证节点".format(self.nodeid_list2[2])) self.ppos_noconsensus_3.createStaking(0, self.account_list[2], self.nodeid_list2[2], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("钱包4 {}委托到3 {}".format(self.account_list[3], self.account_list[2])) value = 100 self.ppos_noconsensus_4.delegate(0, self.nodeid_list2[2], value) log.info("查询当前节点质押信息") msg = self.ppos_noconsensus_3.getCandidateInfo(self.nodeid_list2[2]) stakingBlockNum = msg["Data"]["StakingBlockNum"] log.info("发起质押的区高{}".format(stakingBlockNum)) delegate_value = 20 self.ppos_noconsensus_4.unDelegate(stakingBlockNum, self.nodeid_list2[2], delegate_value) log.info("查询钱包{}的委托信息".format(self.account_list[3])) msg = self.ppos_noconsensus_4.getDelegateInfo(stakingBlockNum, self.account_list[3], self.nodeid_list2[2]) print(msg) data = msg["Data"] """不清楚msg["Data"]对应value是str类型,需要再转字典""" data = json.loads(data) print(data) assert Web3.toChecksumAddress(data["Addr"]) == self.account_list[3] assert data["NodeId"] == self.nodeid_list2[2] print(data["ReleasedHes"]) value = value - delegate_value result_value = Web3.toWei(value, "ether") assert data["ReleasedHes"] == result_value @allure.title("大于委托金额赎回") @pytest.mark.P0 def test_unDelegate_iff(self): """ 验证 大于委托金额赎回 """ msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[2]) if msg['Data'] == "": log.info("质押节点3成为验证节点") self.ppos_noconsensus_3.createStaking(0, self.account_list[2], self.nodeid_list2[2], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("钱包4 委托到3 {}") value = 100 self.ppos_noconsensus_4.delegate(0, self.nodeid_list2[2], value) msg = self.ppos_noconsensus_3.getCandidateInfo(self.nodeid_list2[2]) stakingBlockNum = msg["Data"]["StakingBlockNum"] msg = self.ppos_noconsensus_4.getDelegateInfo(stakingBlockNum, self.account_list[3], self.nodeid_list2[2]) data = msg["Data"] """不清楚msg["Data"]对应value是str类型,需要再转字典""" data = json.loads(data) releasedHes = data["ReleasedHes"] releasedHes = Web3.fromWei(releasedHes, 'ether') log.info("委托钱包可赎回的金额单位eth:{}".format(releasedHes)) delegate_value = releasedHes + 20 msg = self.ppos_noconsensus_4.unDelegate(stakingBlockNum, self.nodeid_list2[2], delegate_value) assert msg["Status"] == False err_msg = "withdrewDelegate err: The von of delegate is not enough" assert err_msg in msg["ErrMsg"] @allure.title("赎回全部委托的金额与赎回金额为0") @pytest.mark.P1 def test_unDelegate_all(self): """ 验证 赎回全部委托的金额 验证 赎回金额为0 """ log.info("转账到钱包1") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[0]) log.info("转账到钱包2") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[1]) log.info("节点1质押成为验证人") self.ppos_noconsensus_1.createStaking(0, self.account_list[0], self.nodeid_list2[0], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0]) stakingBlockNum = msg["Data"]["StakingBlockNum"] log.info("查询节点质押信息,质押块高{}".format(stakingBlockNum)) log.info("钱包2进行委托") value = 80 self.ppos_noconsensus_2.delegate(0, self.nodeid_list2[0], value) msg = self.ppos_noconsensus_2.getDelegateInfo(stakingBlockNum, self.account_list[1], self.nodeid_list2[0]) data = msg["Data"] """不清楚msg["Data"]对应value是str类型,需要再转字典""" data = json.loads(data) log.info(data) releasedHes = data["ReleasedHes"] releasedHes = Web3.fromWei(releasedHes, 'ether') log.info("委托钱包可赎回的金额单位eth:{}".format(releasedHes)) account_before = self.eth.getBalance(self.account_list[1]) msg = self.ppos_noconsensus_2.unDelegate(stakingBlockNum, self.nodeid_list2[0], releasedHes) assert msg["Status"] == True account_after = self.eth.getBalance(self.account_list[1]) result_value = account_after - account_before ##为什么会赎回多那么多钱?1000000000000159999910972325109760 assert account_before + data["ReleasedHes"] > account_after assert 70 < Web3.fromWei(result_value, "ether") < releasedHes, "赎回的金额减去gas应在这范围" log.info("全部委托金额赎回后,查询委托金额".format(account_after)) msg = self.ppos_noconsensus_2.getDelegateInfo(stakingBlockNum, self.account_list[1], self.nodeid_list2[0]) log.info(msg) """如果全部金额赎回,再查getDelegateInfo返回数据为空""" assert msg["Status"] == False assert msg["ErrMsg"] == "Delegate info is not found" @allure.title("多次委托,多次赎回") @pytest.mark.P1 def test_multiple_delegate_undelegate(self): """ 验证多次委托,多次赎回 """ log.info("转账到钱包5") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[4]) log.info("转账到钱包6") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[5]) msg = self.ppos_noconsensus_5.getCandidateInfo(self.nodeid_list2[4]) if msg['Data'] == "": log.info("质押节点5成为验证节点") self.ppos_noconsensus_5.createStaking(0, self.account_list[4], self.nodeid_list2[4], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) msg = self.ppos_noconsensus_5.getCandidateInfo(self.nodeid_list2[4]) stakingBlockNum = msg["Data"]["StakingBlockNum"] log.info("查询节点质押信息回去质押块高{}".format(stakingBlockNum)) value_list = [10, 11, 12, 13, 14, 15, 16, 17, 18] for value in value_list: log.info("钱包6 委托金额{}".format(value)) self.ppos_noconsensus_6.delegate(0, self.nodeid_list2[4], value) msg = self.ppos_noconsensus_6.getDelegateInfo(stakingBlockNum, self.account_list[5], self.nodeid_list2[4]) log.info(msg) data = json.loads(msg["Data"]) releasedHes = Web3.fromWei(data["ReleasedHes"], 'ether') log.info("查询委托的金额{}eth".format(releasedHes)) delegate_value = 0 for i in value_list: delegate_value = i + delegate_value assert delegate_value == releasedHes, "委托的金额与查询到的金额不一致" for value in value_list: log.info("赎回的金额:{}eth".format(value)) log.info("钱包6进行赎回") self.ppos_noconsensus_6.unDelegate(stakingBlockNum, self.nodeid_list2[4], value) log.info("赎回委托金额后查询委托信息") msg = self.ppos_noconsensus_6.getDelegateInfo(stakingBlockNum, self.account_list[5], self.nodeid_list2[4]) """如果全部金额赎回,再查getDelegateInfo返回数据为空""" log.info(msg)
class TestAddstaking(): node_yml_path = conf.NODE_YML node_info = get_node_info(node_yml_path) rpc_list, enode_list, nodeid_list, ip_list, port_list = node_info.get( 'collusion') rpc_list2, enode_list2, nodeid_list2, ip_list2, port_list2 = node_info.get( 'nocollusion') address = Web3.toChecksumAddress(conf.ADDRESS) pwd = conf.PASSWORD privatekey = conf.PRIVATE_KEY account_list = conf.account_list privatekey_list = conf.privatekey_list externalId = "1111111111" nodeName = "platon" website = "https://www.test.network" details = "supper node" programVersion = 1792 illegal_nodeID = conf.illegal_nodeID genesis_path = conf.GENESIS_TMP """替换config.json""" get_config_data() config_json_path = conf.PLATON_CONFIG_PATH config_dict = LoadFile(config_json_path).get_data() amount_delegate = Web3.fromWei( int(config_dict['EconomicModel']['Staking']['MinimumThreshold']), 'ether') amount = Web3.fromWei( int(config_dict['EconomicModel']['Staking']['StakeThreshold']), 'ether') def setup_class(self): self.auto = AutoDeployPlaton() self.auto.start_all_node(self.node_yml_path) self.genesis_dict = LoadFile(self.genesis_path).get_data() self.chainid = int(self.genesis_dict["config"]["chainId"]) self.ppos_link = Ppos(self.rpc_list[0], self.address, self.chainid) self.ppos_link1 = Ppos(self.rpc_list[1], self.address, self.chainid) self.w3_list = [connect_web3(url) for url in self.rpc_list] """用新的钱包地址和未质押过的节点id封装对象""" self.ppos_noconsensus_1 = Ppos(self.rpc_list[0], self.account_list[0], self.chainid, privatekey=self.privatekey_list[0]) self.ppos_noconsensus_2 = Ppos(self.rpc_list[0], self.account_list[1], self.chainid, privatekey=self.privatekey_list[1]) self.ppos_noconsensus_3 = Ppos(self.rpc_list[0], self.account_list[2], self.chainid, privatekey=self.privatekey_list[2]) self.ppos_noconsensus_4 = Ppos(self.rpc_list[0], self.account_list[3], self.chainid, privatekey=self.privatekey_list[3]) self.ppos_noconsensus_5 = Ppos(self.rpc_list[0], self.account_list[4], self.chainid, privatekey=self.privatekey_list[4]) self.ppos_noconsensus_6 = Ppos(self.rpc_list[0], self.account_list[5], self.chainid, privatekey=self.privatekey_list[5]) self.eth = Eth(self.w3_list[0]) def transaction(self, w3, from_address, to_address=None, value=1000000000000000000000000000000000, gas=91000000, gasPrice=9000000000, pwd="88888888"): """" 转账公共方法 """ personal = Personal(w3) personal.unlockAccount(from_address, pwd, 666666) params = { 'to': to_address, 'from': from_address, 'gas': gas, 'gasPrice': gasPrice, 'value': value } tx_hash = w3.eth.sendTransaction(params) result = w3.eth.waitForTransactionReceipt(HexBytes(tx_hash).hex()) return result def getCandidateList(self): """ 获取实时验证人的nodeID list """ msg = self.ppos_noconsensus_1.getCandidateList() recive_list = msg.get("Data") nodeid_list = [] if recive_list is None: return recive_list else: for node_info in recive_list: nodeid_list.append(node_info.get("NodeId")) return nodeid_list @allure.title("增加质押分别为{amount}") @pytest.mark.P1 @pytest.mark.parametrize('amount', [100, 10000000, 1000000]) def test_add_staking(self, amount): """ 用例id 72 账户余额足够,增加质押成功 用例id 74 验证人在质押锁定期内增持的质押数量不限制 """ log.info("转账到钱包1") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[0]) nodeId = self.nodeid_list2[0] log.info("钱包1做一笔质押") self.ppos_noconsensus_1.createStaking(0, self.account_list[0], nodeId, self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("分别验证增持{}".format(amount)) msg = self.ppos_noconsensus_1.addStaking(nodeId, typ=0, amount=amount) print(msg) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0]) nodeid = msg["Data"]["NodeId"] assert nodeid == self.nodeid_list2[0] if amount == 100: assert Web3.fromWei(msg["Data"]["Shares"], 'ether') == self.amount + 100 if amount == 10000000: assert Web3.fromWei(msg["Data"]["Shares"], 'ether') == self.amount + 100 + 10000000 if amount == 1000000: assert Web3.fromWei( msg["Data"]["Shares"], 'ether') == self.amount + 100 + 10000000 + 1000000 @allure.title("非验证人增持质押") @pytest.mark.P2 def test_not_illegal_addstaking(self): """ 用例id 78 非验证人增持质押 """ log.info("转账到钱包1") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[0]) nodeId = conf.illegal_nodeID msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0]) if msg['Data'] == "": log.info("节点1成为验证人") self.ppos_noconsensus_1.createStaking(0, self.account_list[0], self.nodeid_list2[0], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) msg = self.ppos_noconsensus_1.addStaking(nodeId=nodeId, typ=0, amount=100) assert msg.get("Status") == False, "返回状态错误" assert msg.get("ErrMsg") == 'This candidate is not exist', "非验证人增持质押异常" @allure.title("编辑验证人信息-未成为验证人的nodeID") @pytest.mark.P1 def test_editCandidate_nodeid(self): """ 验证修改未成为验证人的nodeID """ externalId = "88888888" nodeName = "wuyiqin" website = "www://baidu.com" details = "node_1" account = self.account_list[0] nodeid = conf.illegal_nodeID self.transaction(self.w3_list[0], self.address, to_address=self.account_list[1]) log.info("转账到钱包2") nodeId = self.nodeid_list2[1] msg = self.ppos_noconsensus_2.getCandidateInfo(nodeId) if msg['Data'] == "": log.info("节点2成为验证人") msg = self.ppos_noconsensus_2.createStaking( 0, self.account_list[1], nodeId, self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" log.info("节点2修改节点信息") msg = self.ppos_noconsensus_2.updateStakingInfo( account, nodeid, externalId, nodeName, website, details) log.info(msg) assert msg.get("Status") == False, "返回状态错误" @allure.title("编辑验证人信息-参数有效性验证") @pytest.mark.P0 def test_editCandidate(self): """ 用例id 70 编辑验证人信息-参数有效性验证 """ externalId = "88888888" nodeName = "wuyiqin" website = "www://baidu.com" details = "node_1" account = self.account_list[0] self.transaction(self.w3_list[0], self.address, to_address=self.account_list[1]) log.info("转账到钱包2") nodeId = self.nodeid_list2[1] msg = self.ppos_noconsensus_2.getCandidateInfo(nodeId) if msg['Data'] == "": log.info("节点2成为验证人") msg = self.ppos_noconsensus_2.createStaking( 0, self.account_list[1], nodeId, self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" log.info("节点2修改节点信息") msg = self.ppos_noconsensus_2.updateStakingInfo( account, self.nodeid_list2[1], externalId, nodeName, website, details) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" log.info("查看节点2的信息") msg = self.ppos_noconsensus_2.getCandidateInfo(self.nodeid_list2[1]) log.info(msg) assert msg["Data"]["ExternalId"] == externalId assert msg["Data"]["NodeName"] == nodeName assert msg["Data"]["Website"] == website assert msg["Data"]["Details"] == details log.info(self.account_list[0]) BenefitAddress = Web3.toChecksumAddress(msg["Data"]["BenefitAddress"]) log.info(BenefitAddress) assert BenefitAddress == account @allure.title("修改钱包地址,更改后的地址收益正常") @pytest.mark.P2 def test_alter_address(self): """ 修改钱包地址,更改后的地址收益正常 """ log.info("转账到钱包2") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[1]) externalId = "88888888" nodeName = "helloword" website = "www://dddddd.com" details = "node_2" ####没钱的钱包##### account = conf.no_money[0] balance_before = self.eth.getBalance(account) assert balance_before == 0 msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[1]) if msg['Data'] == "": log.info("质押节点2成为验证节点") msg = self.ppos_noconsensus_2.createStaking( 0, self.account_list[0], self.nodeid_list2[1], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" log.info("修改节点2的信息") msg = self.ppos_noconsensus_2.updateStakingInfo( account, self.nodeid_list2[1], externalId, nodeName, website, details) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" log.info("进入第2个结算周期") get_block_number(self.w3_list[0]) log.info("进入第3个结算周期") get_block_number(self.w3_list[0]) balance_after = self.eth.getBalance(account) log.info(balance_after) assert balance_after > 0, "修改后的钱包余额没增加" assert balance_after > balance_before, "地址更改后未发现余额增加" @allure.title("增加质押为{x}") @pytest.mark.P2 @pytest.mark.parametrize('x', [0, (amount_delegate - 1)]) def test_add_staking_zero(self, x): """测试增持金额为0,低于门槛""" log.info("转账到钱包3") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[2]) log.info("质押节点3,成为验证人") nodeId = self.nodeid_list2[2] self.ppos_noconsensus_3.createStaking(0, self.account_list[2], nodeId, self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("节点3增持质押".format(x)) msg = self.ppos_noconsensus_3.addStaking(nodeId, typ=0, amount=x) log.info(msg) assert msg.get("Status") == False, "返回状态错误" @allure.title("账户余额不足增加质押") @pytest.mark.P2 def test_Insufficient_addStaking(self): """ 用例id 73 账户余额不足,增加质押失败 """ log.info("转账到钱包5") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[4]) log.info("质押节点5成为验证人") nodeId = self.nodeid_list2[4] msg = self.ppos_noconsensus_5.createStaking( 0, self.account_list[4], nodeId, self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) assert msg.get("Status") == True, "返回状态错误" amount = 100000000000000000000000000000000000 msg = self.ppos_noconsensus_5.addStaking(nodeId=nodeId, typ=0, amount=amount) assert msg.get("Status") == False, "返回状态错误" msg_string = "The von of account is not enough" assert msg_string in msg.get("ErrMsg"), "余额不足发生质押异常" # @allure.title("验证人已申请退出中,申请增持质押") # def test_quit_and_addstaking(self): # """ # 用例id 77 验证人已申请退出中,申请增持质押 # """ # nodeId = self.nodeid_list2[0] # msg = self.ppos_noconsensus_1.unStaking(nodeId) # assert msg.get("Status") == True, "返回状态错误" # assert msg.get("ErrMsg") == 'ok', "返回消错误" # log.info("{}已经退出验证人.".format(nodeId)) # node_list = self.getCandidateList() # log.info(self.nodeid_list2[0]) # log.info(node_list) # assert self.nodeid_list2[0] not in node_list # """验证人退出后,查询实时验证人列表返回的是空,可能是bug""" # msg = self.ppos_noconsensus_1.addStaking(nodeId, typ=0, amount=100) # assert msg.get("Status") == True, "返回状态错误" # assert msg.get("ErrMsg") == 'This candidate is not exist' @allure.title("验证人已申请退出中,申请增持质押") @pytest.mark.P1 def test_quit_and_addstaking(self): """ 用例id 77 验证人已申请退出中,申请增持质押 """ log.info("转账到钱包4") self.transaction(self.w3_list[0], self.address, to_address=self.account_list[3]) nodeId = self.nodeid_list2[3] log.info("节点4发起质押") msg = self.ppos_noconsensus_4.createStaking( 0, self.account_list[3], nodeId, self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) assert msg.get("Status") == True, "质押失败" log.info("节点4退出质押") msg = self.ppos_noconsensus_4.unStaking(nodeId) # print(msg) assert msg.get("Status") == True, "返回状态错误" assert msg.get("ErrMsg") == 'ok', "返回消息错误" log.info("{}已经退出验证人.".format(nodeId)) node_list = self.getCandidateList() # print(nodeId) # print(node_list) assert nodeId not in node_list msg = self.ppos_noconsensus_1.getCandidateInfo(nodeId) assert msg['Data'] == "" msg = self.ppos_noconsensus_4.addStaking(nodeId, typ=0, amount=100) assert msg.get("Status") == False, "返回状态错误" assert msg.get("ErrMsg") == 'This candidate is not exist'
class TestLockup(): node_yml_path = conf.NODE_YML node_info = get_node_info(node_yml_path) rpc_list, enode_list, nodeid_list, ip_list, port_list = node_info.get( 'collusion') rpc_list2, enode_list2, nodeid_list2, ip_list2, port_list2 = node_info.get( 'nocollusion') address = Web3.toChecksumAddress(conf.ADDRESS) privatekey = conf.PRIVATE_KEY account_list = conf.account_list privatekey_list = conf.privatekey_list externalId = "1111111111" nodeName = "platon" website = "https://www.test.network" details = "supper node" programVersion = 1792 illegal_nodeID = conf.illegal_nodeID genesis_path = conf.GENESIS_TMP get_config_data() config_json_path = conf.PLATON_CONFIG_PATH config_dict = LoadFile(config_json_path).get_data() amount_delegate = Web3.fromWei( int(config_dict['EconomicModel']['Staking']['MinimumThreshold']), 'ether') amount = Web3.fromWei( int(config_dict['EconomicModel']['Staking']['StakeThreshold']), 'ether') def setup_class(self): self.auto = AutoDeployPlaton() self.auto.start_all_node(self.node_yml_path) self.genesis_dict = LoadFile(self.genesis_path).get_data() self.chainid = int(self.genesis_dict["config"]["chainId"]) self.ppos_link = Ppos(self.rpc_list[0], self.address, self.chainid) self.w3_list = [connect_web3(url) for url in self.rpc_list] """用新的钱包地址和未质押过的节点id封装对象""" self.ppos_noconsensus_1 = Ppos(self.rpc_list[0], self.account_list[0], self.chainid, privatekey=self.privatekey_list[0]) self.ppos_noconsensus_2 = Ppos(self.rpc_list[0], self.account_list[1], self.chainid, privatekey=self.privatekey_list[1]) self.ppos_noconsensus_3 = Ppos(self.rpc_list[0], self.account_list[2], self.chainid, privatekey=self.privatekey_list[2]) self.ppos_noconsensus_4 = Ppos(self.rpc_list[0], self.account_list[3], self.chainid, privatekey=self.privatekey_list[3]) self.ppos_noconsensus_5 = Ppos(self.rpc_list[0], self.account_list[4], self.chainid, privatekey=self.privatekey_list[4]) self.ppos_noconsensus_6 = Ppos(self.rpc_list[0], self.account_list[5], self.chainid, privatekey=self.privatekey_list[5]) self.eth = Eth(self.w3_list[0]) def transaction(self, w3, from_address, to_address=None, value=100000000000000000000000000000000, gas=91000000, gasPrice=9000000000, pwd=conf.PASSWORD): """" 转账公共方法,转账1000w eth """ personal = Personal(w3) personal.unlockAccount(from_address, pwd, 666666) params = { 'to': to_address, 'from': from_address, 'gas': gas, 'gasPrice': gasPrice, 'value': value } tx_hash = w3.eth.sendTransaction(params) result = w3.eth.waitForTransactionReceipt(HexBytes(tx_hash).hex()) return result def getCandidateList(self): """ 获取实时验证人的nodeID list """ msg = self.ppos_noconsensus_1.getCandidateList() recive_list = msg.get("Data") nodeid_list = [] if recive_list is None: return recive_list else: for node_info in recive_list: nodeid_list.append(node_info.get("NodeId")) return nodeid_list @allure.title("验证人退回质押金(未到达可解锁期)") @pytest.mark.P0 def test_back_unstaking(self): """ 验证人退回质押金(未到达可解锁期) 质押成为下个周期验证人,退出后,下一个结算周期退出 """ log.info("转账每个钱包") for to_account in self.account_list: self.transaction(self.w3_list[0], self.address, to_address=to_account) log.info("节点1质押金额{} eth".format(self.amount)) self.ppos_noconsensus_1.createStaking(0, self.account_list[0], self.nodeid_list2[0], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) account_1 = self.eth.getBalance(self.account_list[0]) log.info(account_1) get_block_number(self.w3_list[0]) log.info("查询第2个结算周期的验证人") node_list = getVerifierList() log.info(node_list) assert self.nodeid_list2[0] in node_list log.info("节点1在第2结算周期,锁定期申请退回") self.ppos_noconsensus_1.unStaking(nodeId=self.nodeid_list2[0]) """发起退回消耗一定gas""" account_2 = self.eth.getBalance(self.account_list[0]) log.info(account_2) assert account_1 > account_2, "发起退回的交易钱包异常" log.info("进入第3个结算周期") get_block_number(w3=self.w3_list[0]) account_3 = self.eth.getBalance(self.account_list[0]) log.info(account_3) assert account_3 > account_2 assert account_3 - account_2 < Web3.toWei(self.amount, "ether") log.info(account_3 - account_2) node_list = getVerifierList() log.info(node_list) assert self.nodeid_list2[0] not in node_list log.info("进入第4个结算周期") get_block_number(w3=self.w3_list[0]) account_4 = self.eth.getBalance(self.account_list[0]) log.info(account_4) """ 第3个结算周期结束后质押金额已退 """ log.info(account_4 - account_3) assert account_4 - account_3 > Web3.toWei(self.amount, "ether") assert account_4 - account_2 > Web3.toWei(self.amount, "ether") @allure.title("锁定期增加质押与委托") @pytest.mark.P0 def test_lockup_addstaking(self): """ 用例id74 锁定期增加质押 用例id 112 锁定期委托人进行委托 """ log.info("转账每个钱包") for to_account in self.account_list: self.transaction(self.w3_list[0], self.address, to_address=to_account) msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0]) if msg["Data"] == "": log.info("节点1质押金额{} eth".format(self.amount)) self.ppos_noconsensus_1.createStaking(0, self.account_list[0], self.nodeid_list2[0], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("进入锁定期") get_block_number(self.w3_list[0]) value = 100 msg = self.ppos_noconsensus_1.addStaking(self.nodeid_list2[0], 0, value) assert msg.get("Status") == True msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0]) log.info(msg) nodeid = msg["Data"]["NodeId"] log.info("增持后验证质押金额") assert msg["Data"]["Shares"] == 1000100000000000000000000 assert msg["Data"]["Released"] == 1000000000000000000000000 assert msg["Data"]["ReleasedHes"] == 100000000000000000000 assert nodeid == self.nodeid_list2[0] self.ppos_noconsensus_2.delegate(0, self.nodeid_list2[0], 10) msg = self.ppos_noconsensus_2.getCandidateInfo(self.nodeid_list2[0]) log.info(msg) assert msg["Data"]["Shares"] == 1000110000000000000000000 stakingBlockNum = msg["Data"]["StakingBlockNum"] msg = self.ppos_noconsensus_2.getDelegateInfo(stakingBlockNum, self.account_list[1], self.nodeid_list2[0]) data = msg["Data"] data = json.loads(data) assert Web3.toChecksumAddress(data["Addr"]) == self.account_list[1] assert data["NodeId"] == self.nodeid_list2[0] # assert Web3.fromWei(data["ReleasedHes"], 'ether') == 10 @allure.title("验证根据质押+委托金额金额排名") @pytest.mark.P2 def test_taking_cycle_ranking(self): """ 验证根据质押金额排名 """ log.info("转账每个钱包") for to_account in self.account_list: self.transaction(self.w3_list[0], self.address, to_address=to_account) log.info("质押节点2") self.ppos_noconsensus_2.createStaking(0, self.account_list[1], self.nodeid_list2[1], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("质押节点3") self.ppos_noconsensus_3.createStaking(0, self.account_list[2], self.nodeid_list2[2], self.externalId, self.nodeName, self.website, self.details, self.amount + 130, self.programVersion) log.info("进入到下个结算周期") get_block_number(w3=self.w3_list[0]) node_list = getVerifierList() log.info(node_list) msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[1]) log.info(msg) msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[2]) log.info(msg) assert self.nodeid_list2[2] in node_list assert self.nodeid_list2[1] in node_list assert self.nodeid_list2[2] == node_list[0] log.info("钱包4委托节点2 70eth") msg = self.ppos_noconsensus_4.delegate(0, nodeId=self.nodeid_list2[1], amount=70) print(msg) log.info("节点2增加质押61 eth") msg = self.ppos_noconsensus_2.addStaking(self.nodeid_list2[1], 0, amount=61) print(msg) log.info("进入下一个结算周期") get_block_number(self.w3_list[0]) node_list = getVerifierList() log.info(node_list) log.info(self.nodeid_list2[1]) log.info(self.nodeid_list2[2]) """预期节点2排在节点3之前""" msg = self.ppos_noconsensus_2.getCandidateInfo(self.nodeid_list2[1]) log.info(msg) msg = self.ppos_noconsensus_3.getCandidateInfo(self.nodeid_list2[2]) log.info(msg) assert node_list[0] == self.nodeid_list2[1] assert node_list[1] == self.nodeid_list2[2] @allure.title("质押相等的金额,按照时间先早排序") @pytest.mark.P1 def test_same_amount_cycle(self): """ 质押相等的金额,按照时间先早排序 """ self.auto = AutoDeployPlaton() self.auto.start_all_node(self.node_yml_path) log.info("转账每个钱包") for to_account in self.account_list: self.transaction(self.w3_list[0], self.address, to_address=to_account) log.info("质押节点4金额200eth") self.ppos_noconsensus_4.createStaking(0, self.account_list[3], self.nodeid_list2[3], self.externalId, self.nodeName, self.website, self.details, self.amount + 200, self.programVersion) log.info("质押节点5金额200eth") self.ppos_noconsensus_5.createStaking(0, self.account_list[4], self.nodeid_list2[4], self.externalId, self.nodeName, self.website, self.details, self.amount + 200, self.programVersion) log.info("进入到下个结算周期") get_block_number(self.w3_list[0]) node_list = getVerifierList() log.info(node_list) log.info(self.nodeid_list2[3]) log.info(self.nodeid_list2[4]) assert node_list[0] == self.nodeid_list2[3] assert node_list[1] == self.nodeid_list2[4] @allure.title("验证人申请退回所有质押金(包含初始质押金和当前结算期内质押金)") @pytest.mark.P2 def test_unstaking_all(self): log.info("转账每个钱包") for to_account in self.account_list: self.transaction(self.w3_list[0], self.address, to_address=to_account) value_before = self.eth.getBalance(self.account_list[5]) log.info(value_before) self.ppos_noconsensus_6.createStaking(0, self.account_list[5], self.nodeid_list2[5], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("进入第2个结算周期") get_block_number(self.w3_list[0]) log.info("节点6增持金额") self.ppos_noconsensus_6.addStaking(self.nodeid_list2[5], 0, self.amount + 1000) log.info("进入第3个结算周期") get_block_number(self.w3_list[0]) log.info("节点6发起退回") self.ppos_noconsensus_6.unStaking(nodeId=self.nodeid_list2[5]) log.info("进入第4个结算周期") get_block_number(self.w3_list[0]) value = self.eth.getBalance(self.account_list[5]) log.info(value) assert value < value_before, "钱还在锁定期,预期未退回,实际异常" log.info("进入第5个结算周期") get_block_number(self.w3_list[0]) value_after = self.eth.getBalance(self.account_list[5]) log.info(value_after) log.info(value_after - value_before) amount_sum = self.amount * 2 + 1000 log.info(Web3.toWei(amount_sum, "ether")) assert value_after > value_before, "出块奖励异常" assert value_after > value + Web3.toWei( amount_sum, "ether"), "解锁期的余额大于锁定期的余额+质押+增持金额,但是发生异常" @allure.title("根据金额排名,从高到低排名") @pytest.mark.P0 def test_ranking(self): """ 测试根据金额排名,从高到低排名 """ self.auto = AutoDeployPlaton() self.auto.start_all_node(self.node_yml_path) log.info("转账每个钱包") for to_account in self.account_list: self.transaction(self.w3_list[0], self.address, to_address=to_account) self.ppos_noconsensus_1.createStaking(0, self.account_list[0], self.nodeid_list2[0], self.externalId, self.nodeName, self.website, self.details, self.amount + 50, self.programVersion) self.ppos_noconsensus_2.createStaking(0, self.account_list[1], self.nodeid_list2[1], self.externalId, self.nodeName, self.website, self.details, self.amount + 40, self.programVersion) self.ppos_noconsensus_3.createStaking(0, self.account_list[2], self.nodeid_list2[2], self.externalId, self.nodeName, self.website, self.details, self.amount + 30, self.programVersion) self.ppos_noconsensus_4.createStaking(0, self.account_list[3], self.nodeid_list2[3], self.externalId, self.nodeName, self.website, self.details, self.amount + 20, self.programVersion) self.ppos_noconsensus_5.createStaking(0, self.account_list[4], self.nodeid_list2[4], self.externalId, self.nodeName, self.website, self.details, self.amount + 10, self.programVersion) self.ppos_noconsensus_6.createStaking(0, self.account_list[5], self.nodeid_list2[5], self.externalId, self.nodeName, self.website, self.details, self.amount, self.programVersion) log.info("进入下个周期") get_block_number(self.w3_list[0]) node_list = getVerifierList() log.info(node_list) """根据config配置验证人数""" # assert self.nodeid_list2[5] not in node_list assert node_list[0] == self.nodeid_list2[0] assert node_list[1] == self.nodeid_list2[1] assert node_list[2] == self.nodeid_list2[2] assert node_list[3] == self.nodeid_list2[3] assert node_list[4] == self.nodeid_list2[4] assert node_list[5] == self.nodeid_list2[-1]