Exemple #1
0
class TestPledge():
    node_yml_path = conf.PPOS_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"
    amount = 1000000
    programVersion = 1792
    illegal_nodeID = conf.illegal_nodeID


    def setup_class(self):
        self.ppos_link = Ppos(
            self.rpc_list[0], self.address)
        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], privatekey= self.privatekey_list[0])
        self.ppos_noconsensus_2 = Ppos(self.rpc_list[0], self.account_list[1], privatekey=self.privatekey_list[1])
        self.ppos_noconsensus_3 = Ppos(self.rpc_list[0], self.account_list[2], privatekey=self.privatekey_list[2])
        for to_account in self.account_list:
            self.transaction(self.w3_list[0],self.address,to_account)

    def transaction(self,w3, from_address, to_address=None, value=100000000000000000000000000000000,
                    gas=91000000, gasPrice=9000000000):
        personal = Personal(self.w3_list[0])
        personal.unlockAccount(self.address, self.pwd, 666666)
        params = {
            'to': to_address,
            'from': from_address,
            'gas': gas,
            'gasPrice': gasPrice,
            'value': value
        }
        tx_hash = w3.eth.sendTransaction(params)
        return tx_hash

    def test_initial_identifier(self):
        """
        用例id 54,55
        测试验证人参数有效性验证
        """
        recive = self.ppos_link.getVerifierList()
        recive_list = recive.get("Data")
        nodeid_list=[]
        # 查看查询当前结算周期的验证人队列
        for node_info in recive_list:
            nodeid_list.append(node_info.get("NodeId"))
            StakingAddress = node_info.get("StakingAddress")
            assert StakingAddress == self.address,"内置钱包地址错误{}".format(StakingAddress)
        # print(recive)
        # print(nodeid_list)
        assert nodeid_list == nodeid_list, "正常的nodeID列表{},异常的nodeID列表{}".format(nodeid_list,recive_list)

    def test_initial_cannot_entrust(self):
        """
        用例id 56 初始验证人不能接受委托
        """
        msg = self.ppos_link.delegate(typ = 1,nodeId=self.nodeid_list[0],amount=50)
        assert msg.get("Status") == False ,"返回状态错误"
        assert msg.get("ErrMsg") == 'This candidate is not allow to delegate',"返回提示语错误"

    def test_initial_add_pledge(self):
        """
        用例id 57 初始验证人增持质押
        """
        msg = self.ppos_link.addStaking(nodeId=self.nodeid_list[0],typ=0,amount=1000)
        print(msg)
        assert msg.get("Status") == True
        assert msg.get("ErrMsg") == 'ok'

    def test_initial_quit(self):
        """
        用例id 58 初始验证人退出
        """
        msg = self.ppos_link.unStaking(nodeId=self.nodeid_list[0])
        print(self.nodeid_list[0])
        assert msg.get("Status") ==True
        assert msg.get("ErrMsg") == 'ok'
        """暂时没配置参数所以还要调整"""
        time.sleep(10)
        recive = self.ppos_link.getCandidateList()
        recive_list = recive.get("Data")
        for node_info in recive_list:
            # print(node_info)
            assert node_info.get("NodeId") != self.nodeid_list[0]

    def test_staking(self):
        """
        用例 62 非初始验证人做质押
        """
        nodeId = self.nodeid_list2[0]
        """非共识节点做一笔质押"""
        msg = self.ppos_noconsensus_1.createStaking(0, self.account_list[1],
                                           nodeId,self.externalId,self.nodeName, self.website,self.details,
                                                    self.amount,self.programVersion)
        print(msg)
        assert msg.get("Status") ==True
        assert msg.get("ErrMsg") == 'ok'
        """暂时没配置参数所以还要调整"""
        ##查看实时验证人信息
        recive = self.ppos_noconsensus_1.getCandidateList()
        recive_list = recive.get("Data")
        nodeid_list = []
        for node_info in recive_list:
            nodeid_list.append(node_info.get("NodeId"))
        assert self.nodeid_list2[0] in nodeid_list,"非初始验证人质押失败"

    def test_staking_zero(self):
        """
        用例 64 非初始验证人做质押,质押金额小于最低门槛
        """
        benifitAddress = self.account_list[1]
        nodeId = self.nodeid_list2[0]
        """非共识节点做一笔质押"""
        amount = 100000
        msg = self.ppos_noconsensus_1.createStaking(0, benifitAddress,
                                           nodeId,self.externalId,self.nodeName, self.website, self.details,amount,self.programVersion)
        print(msg)
        assert msg.get("Status") ==False

    def test_not_illegal_addstaking(self):
        """
        用例id 78 非初始验证人成为验证人后,增持质押
        """
        nodeId = conf.illegal_nodeID
        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'

    def test_illegal_pledge(self):
        """
        用例id 63 非链上的nodeID去质押
        """
        benifitAddress = self.account_list[1]
        msg = self.ppos_noconsensus_2.createStaking(0, benifitAddress,self.illegal_nodeID,self.externalId,
                                           self.nodeName, self.website, self.details,self.amount,self.programVersion)
        print(msg)
        assert msg.get("Status") ==True
        assert msg.get("ErrMsg") == 'ok'

    def test_getCandidateInfo(self):
        """
        用例69 查询验证人信息,调用质押节点信息
        """
        msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0])
        nodeid = msg["Data"]["NodeId"]
        """验证质押金额"""
        assert  msg["Data"]["Shares"] == 1000000000000000000000000
        assert nodeid == self.nodeid_list[0]
        msg = self.ppos_link.getCandidateInfo(self.nodeid_list[0])
        # print(msg)
        nodeid = msg["Data"]["NodeId"]
        assert nodeid == self.nodeid_list[0]

    # @pytest.mark.parametrize("externalId,nodeName,website,details",[("88888888","jay_wu","https://baidu.com,node_1"),
    #                                                                       ("","","","")])
    # def test_editCandidate(self,externalId,nodeName,website,details):
    #     """
    #     用例id 70 编辑验证人信息-参数有效性验证
    #     """
    #     msg = self.ppos_noconsensus_1.updateStakingInfo(self.account_list[0], self.nodeid_list2[0],
    #                                                     externalId, nodeName, website, details)
    #     print(msg)
    #     msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0])
    #     print(msg)

    def test_editCandidate(self):
        """
        用例id 70 编辑验证人信息-参数有效性验证
        """
        externalId = "88888888"
        nodeName = "jay_wu"
        website = "www://baidu.com"
        details = "node_1"
        msg = self.ppos_noconsensus_1.updateStakingInfo(self.account_list[0], self.nodeid_list2[0],
                                                        externalId, nodeName, website, details)
        assert msg.get("Status") ==True
        assert msg.get("ErrMsg") == 'ok'
        msg = self.ppos_noconsensus_1.getCandidateInfo(self.nodeid_list2[0])
        print(msg)
        assert msg["Data"]["ExternalId"] == externalId
        assert msg["Data"]["nodeName"] == nodeName
        assert msg["Data"]["website"] == website
        assert msg["Data"]["details"] == details

    @pytest.mark.parametrize('amount', [(100),(10000000),(1000000)])
    def test_add_staking(self,amount):
        """
        用例id 72 账户余额足够,增加质押成功
        用例id 74 验证人在质押锁定期内增持的质押数量不限制
        """
        nodeId = self.nodeid_list2[0]
        msg = self.ppos_noconsensus_1.addStaking(nodeId,typ=0,amount=amount)
        assert msg.get("Status") == True
        assert msg.get("ErrMsg") == 'ok'

    def test_add_staking_zero(self):
        """测试增持金额为0"""
        nodeId = self.nodeid_list2[0]
        msg = self.ppos_noconsensus_1.addStaking(nodeId,typ=0,amount=0)
        assert msg.get("Status") == False

    def test_Insufficient_delegate(self):
        """
        用例id 73 账户余额不足,增加质押失败
        """
        amount = 100000000000000000000000000000000000
        nodeId = self.nodeid_list2[0]
        msg = self.ppos_noconsensus_1.addStaking(nodeId=nodeId,typ=0,amount=amount)
        # print(msg)
        assert msg.get("Status") == False
        assert msg.get("ErrMsg") == 'The von of account is not enough'

    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'
        time.sleep(2)
        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'

    # def test_getDelegateInfo(self):
    #     """
    #     用例id 94 查询委托参数验证
    #     """
    #     stakingBlockNum = ""
    #     msg = self.ppos_noconsensus_1.getDelegateInfo(stakingBlockNum,delAddr,nodeId)

    def test_illege_delegate(self):
        """
        用例id 首次委托金额必须大于10 eth
        """
        amount = 9
        msg = self.ppos_noconsensus_2.delegate(0,self.nodeid_list2[0],amount)
        print(msg)
        assert msg.get("Status") == False
        assert msg.get("ErrMsg") == "Delegate deposit too low"
        amount = 0
        msg = self.ppos_noconsensus_2.delegate(0, self.nodeid_list2[0], amount)
        assert msg.get("Status") == False
        assert msg.get("ErrMsg") == "This amount is illege"

    def test_delegate(self):
        """
        用例95 委托成功,查询节点信息,金额
        用例id 98 委托人与验证人验证,验证人存在
        """
        amount = 1000
        msg = self.ppos_noconsensus_2.delegate(0,self.illegal_nodeID,amount)
        assert msg.get("Status") == True
        assert msg.get("ErrMsg") == 'ok'

    def test_getDelegateInfo(self):
        """
        用例id 94 查询委托参数验证
        """
        msg = self.ppos_noconsensus_2.getCandidateInfo(self.illegal_nodeID)
        print(msg)
        assert msg["Data"]["NodeId"] == self.illegal_nodeID
        """查询当前候选人总共质押加被委托的von数目"""
        assert msg["Data"]["Shares"] == 1001000000000000000000000
        assert msg["Data"]["ReleasedHes"] == 1000000000000000000000000
        """通过查询"""
        stakingBlockNum = msg["Data"]["StakingBlockNum"]
        # print(stakingBlockNum)
        msg = self.ppos_noconsensus_2.getDelegateInfo(stakingBlockNum,self.account_list[1],self.illegal_nodeID)
        print(msg)
        assert msg["Data"]["Addr"] == self.account_list[1]
        assert msg["Data"]["NodeId"] == self.illegal_nodeID
        assert msg["Data"]["ReleasedHes"] == 1000000000000000000000

    def test_insufficient_delegate(self):
        """
        用例96 余额不足委托失败
        """
        amount = 100000000000000000000000000
        msg = self.ppos_noconsensus_2.delegate(0,self.illegal_nodeID,amount)
        # print(msg)
        assert msg.get("Status") == False
        assert msg.get("ErrMsg") == 'Delegate failed: The von of account is not enough'

    def test_not_nodeId_delegate(self):
        """
        用例72 验证人不存在进行委托
        """
        amount = 1000
        msg = self.ppos_noconsensus_2.delegate(0,self.nodeid_list2[2],amount)
        print(msg)
        assert msg.get("Status") == False
        assert msg.get("ErrMsg") == 'This candidate is not exist'

    def test_restrictingPlan_delegate(self):
        """
        用例id 101 委托人使用锁仓Token进行委托
        """
        pass

    def test_restrictingPlan_insufficient_delegate(self):
        """
        用例id 103委托人使用锁仓Token(余额不足)进行委托
        """

    def
    def test_unlock_point_pledge_amount(self):
        '''
        到达解锁时间点,锁仓金额质押后在解锁期之后再申请退回质押金
        :return:
        '''
        nodeId = CommonMethod.get_no_candidate_list (self)
        url = CommonMethod.link_list (self)
        platon_ppos = Ppos (url, self.address, self.chainid)
        address1, private_key1 = CommonMethod.read_private_key_list ()
        address2, private_key2 = CommonMethod.read_private_key_list ()

        # 签名转账
        result = platon_ppos.send_raw_transaction ('', Web3.toChecksumAddress (self.address),
                                                   Web3.toChecksumAddress (address1),
                                                   self.base_gas_price, self.base_gas, self.value, self.privatekey)
        return_info = platon_ppos.eth.waitForTransactionReceipt (result)
        assert return_info is not None,"转账:{}失败".format(self.value)

        # 给锁仓账号转手续费
        result = platon_ppos.send_raw_transaction ('', Web3.toChecksumAddress (self.address),
                                                   Web3.toChecksumAddress (address2),
                                                   self.base_gas_price, self.base_gas, self.value,
                                                   self.privatekey)
        return_info = platon_ppos.eth.waitForTransactionReceipt (result)
        assert return_info is not None, "转账:{}失败".format (self.value)
        Balance = platon_ppos.eth.getBalance(address1)
        log.info("{}发起锁仓账户余额:{}".format(address1,Balance))
        Balance1 = platon_ppos.eth.getBalance (address1)
        log.info ("{}发起质押账号余额:{}".format (address2, Balance1))

        # 创建锁仓计划
        lockupamoutn = 900
        loukupbalace = Web3.toWei (lockupamoutn, 'ether')
        plan = [{'Epoch': 1, 'Amount': loukupbalace}]
        result = platon_ppos.CreateRestrictingPlan (address2, plan, privatekey=private_key1,
                                                    from_address=address1, gasPrice=self.base_gas_price,
                                                    gas=self.staking_gas)
        assert result['Status'] == True, "创建锁仓计划返回的状态:{},用例失败".format (result['Status'])
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "查询锁仓计划返回的状态:{},用例失败".format (result['Status'])
        info = json.loads(RestrictingInfo['Data'])
        assert info['balance'] == loukupbalace,"锁仓计划可用余额:{},有误".format(info['balance'])
        balance2 = platon_ppos.eth.getBalance (address1)
        log.info ("{}创建锁仓后余额:{}".format (address1,balance2))

        # 申请质押节点
        version = get_version (platon_ppos)
        amount = 800
        result = platon_ppos.createStaking (1, address2, nodeId, 'externalId', 'nodeName', 'website', 'details',
                                            amount, version, privatekey=private_key2, from_address=address2,
                                            gasPrice=self.base_gas_price, gas=self.staking_gas)
        assert result['Status'] == True, "申请质押返回的状态:{},{},用例失败".format (result['Status'], result['ErrMsg'])
        balance3 = platon_ppos.eth.getBalance (address2)
        log.info ("{}质押完账户余额:{}".format (address2,balance3))
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        assert dict_Info['balance'] == Web3.toWei(lockupamoutn - amount,'ether'), "锁仓金额:{}有误".format (dict_Info['balance'])

        # 到达解锁期之后锁仓账号余额
        CommonMethod.get_next_settlement_interval (self)
        balance4 = platon_ppos.eth.getBalance (address2)
        log.info("到达解锁期后预期账户余额:{}".format(balance3 + Web3.toWei(lockupamoutn - amount,'ether')))
        log.info("到达解锁期后实际账户余额:{}".format (balance4))
        assert balance4 == balance3 + Web3.toWei(lockupamoutn - amount,'ether'),"锁仓账户金额:{} 有误".format(balance4)
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        assert dict_Info['balance'] == 0, "锁仓金额:{}有误".format (dict_Info['balance'])
        assert dict_Info['symbol'] == True,"锁仓的状态:{} 有误".format(dict_Info['symbol'])
        assert dict_Info['debt'] == Web3.toWei(amount,'ether'),"欠释放锁仓金额:{} 有误".format(dict_Info['debt'])

        # 申请退回质押金
        result = platon_ppos.unStaking (nodeId, privatekey=private_key2, from_address=address2,
                                        gasPrice=self.base_gas_price, gas=self.staking_gas)
        assert result['Status'] == True, "申请质押退回质押金返回的状态:{},用例失败".format (result['Status'])
        balance5 = platon_ppos.eth.getBalance (address2)
        log.info ("到达解锁期申请退回质押金后账户余额:{}".format (balance5))

        # 等待两个结算周期后查询锁仓账号情况
        CommonMethod.get_next_settlement_interval (self,3)
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        balance6 = platon_ppos.eth.getBalance (address2)
        log.info ("到达解锁期退回质押金后预期账户金额:{}".format (balance5 + loukupbalace))
        log.info ("到达解锁期退回质押金后实际账户余额:{}".format (balance6))
        assert balance6 == balance5 + loukupbalace, "锁仓账户金额:{} 有误".format (balance6)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        assert dict_Info['balance'] == 0, "锁仓金额:{}有误".format (dict_Info['balance'])
        assert dict_Info['symbol'] == False, "锁仓的状态:{} 有误".format (dict_Info['symbol'])
        assert dict_Info['debt'] == 0, "欠释放锁仓金额:{} 有误".format (dict_Info['debt'])
Exemple #3
0
    def test_lockup_Withdrawal_of_pledge(self):
        '''
        锁仓账户申请完质押后又退回质押金
        :return:
        '''
        nodeId = CommonMethod.get_no_candidate_list(self)
        url = CommonMethod.link_list(self)
        platon_ppos = Ppos(url, self.address, self.chainid)
        address1, private_key1 = CommonMethod.read_private_key_list()
        address2, private_key2 = CommonMethod.read_private_key_list()

        # 签名转账
        result = platon_ppos.send_raw_transaction(
            '', Web3.toChecksumAddress(self.address),
            Web3.toChecksumAddress(address1), self.base_gas_price,
            self.base_gas, self.value, self.privatekey)
        return_info2 = platon_ppos.eth.waitForTransactionReceipt(result)
        assert return_info2 is not None, "转账:{}失败".format(self.value)

        # 创建锁仓计划
        balance = platon_ppos.eth.getBalance(address1)
        log.info("发起锁仓账户余额:{}".format(balance))
        loukupbalace = Web3.toWei(800, 'ether')
        plan = [{'Epoch': 2, 'Amount': loukupbalace}]
        result = platon_ppos.CreateRestrictingPlan(
            address2,
            plan,
            privatekey=private_key1,
            from_address=address1,
            gasPrice=self.base_gas_price,
            gas=self.staking_gas)
        assert result['Status'] == True, "创建锁仓计划返回的状态:{},用例失败".format(
            result['Status'])
        # RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        # assert RestrictingInfo['Status'] == True, "查询锁仓计划返回的状态:{},用例失败".format (result['Status'])

        lockup_before = platon_ppos.eth.getBalance(
            conf.FOUNDATIONLOCKUPADDRESS)
        log.info("申请质押节点之前锁仓账户金额:{}".format(lockup_before))
        Staking_before = platon_ppos.eth.getBalance(conf.STAKINGADDRESS)
        log.info("申请质押节点之前Staking账户金额:{}".format(Staking_before))
        RestrictingInfo = platon_ppos.GetRestrictingInfo(address2)
        RestrictingInfo = json.loads(RestrictingInfo['Data'])
        log.info("锁仓计划可用余额:{}".format(RestrictingInfo['balance']))

        # 签名转账
        result = platon_ppos.send_raw_transaction(
            '', Web3.toChecksumAddress(self.address),
            Web3.toChecksumAddress(address2), self.base_gas_price,
            self.base_gas, self.value, self.privatekey)
        return_info2 = platon_ppos.eth.waitForTransactionReceipt(result)
        assert return_info2 is not None, "转账:{}失败".format(self.value)

        # 发起质押
        version = get_version(platon_ppos)
        amount = 800
        result = platon_ppos.createStaking(1,
                                           address2,
                                           nodeId,
                                           'externalId',
                                           'nodeName',
                                           'website',
                                           'details',
                                           amount,
                                           version,
                                           privatekey=private_key2,
                                           from_address=address2,
                                           gasPrice=self.base_gas_price,
                                           gas=self.staking_gas)
        assert result['Status'] == True, "申请质押返回的状态:{},用例失败".format(
            result['Status'])

        #查询每个账户余额
        CandidateInfo = platon_ppos.getCandidateInfo(nodeId)
        log.info("质押节点信息:{}".format(CandidateInfo))
        lockup_Back_before = platon_ppos.eth.getBalance(
            conf.FOUNDATIONLOCKUPADDRESS)
        log.info("退回质押金之前锁仓账户金额:{}".format(lockup_Back_before))
        Staking_Back_before = platon_ppos.eth.getBalance(conf.STAKINGADDRESS)
        log.info("退回质押金之前Staking账户金额:{}".format(Staking_Back_before))
        RestrictingInfo = platon_ppos.GetRestrictingInfo(address2)
        RestrictingInfo = json.loads(RestrictingInfo['Data'])
        log.info("锁仓计划可用余额:{}".format(RestrictingInfo['balance']))

        #申请退回质押金
        result = platon_ppos.unStaking(nodeId,
                                       privatekey=private_key2,
                                       from_address=address2,
                                       gasPrice=self.base_gas_price,
                                       gas=self.staking_gas)
        assert result['Status'] == True, "申请质押退回质押金返回的状态:{},用例失败".format(
            result['Status'])
        lockup_end = platon_ppos.eth.getBalance(conf.FOUNDATIONLOCKUPADDRESS)
        log.info("退回质押金之后锁仓账户金额:{}".format(lockup_end))
        Staking_end = platon_ppos.eth.getBalance(conf.STAKINGADDRESS)
        log.info("退回质押金之后Staking账户金额:{}".format(Staking_end))
        RestrictingInfo = platon_ppos.GetRestrictingInfo(address2)
        RestrictingInfo = json.loads(RestrictingInfo['Data'])
        log.info("锁仓计划可用余额:{}".format(RestrictingInfo['balance']))
        assert lockup_end == lockup_before, "质押金退回后锁仓金额:{}有误".format(
            lockup_end)
        assert Staking_end == Staking_before, "质押金退回后Staking金额:{}有误".format(
            Staking_end)
        assert RestrictingInfo[
            'balance'] == loukupbalace, "锁仓计划金额:{}有误".format(
                RestrictingInfo['balance'])