class TestDposinit:
    address = conf.ADDRESS
    pwd = conf.PASSWORD
    abi = conf.DPOS_CONTRACT_ABI
    cbft_json_path = conf.CBFT2
    node_yml_path = conf.PPOS_NODE_TEST_YML
    file = conf.CASE_DICT
    privatekey = conf.PRIVATE_KEY
    base_gas_price = 60000000000000
    base_gas = 21000
    staking_gas = base_gas + 32000 + 6000 + 100000
    transfer_gasPrice = Web3.toWei(1, 'ether')
    transfer_gas = 210000000
    value = 1000
    chainid = 120
    ConsensusSize = 150
    time_interval = 10
    initial_amount = {
        'FOUNDATION': 905000000000000000000000000,
        'FOUNDATIONLOCKUP': 20000000000000000000000000,
        'STAKING': 25000000000000000000000000,
        'INCENTIVEPOOL': 45000000000000000000000000,
        'DEVELOPERS': 5000000000000000000000000
    }

    def start_init(self):
        # 修改config参数
        CommonMethod.update_config(self, 'EconomicModel', 'Common',
                                   'ExpectedMinutes', 3)
        CommonMethod.update_config(self, 'EconomicModel', 'Common',
                                   'PerRoundBlocks', 5)
        CommonMethod.update_config(self, 'EconomicModel', 'Common',
                                   'ValidatorCount', 10)
        CommonMethod.update_config(self, 'EconomicModel', 'Staking',
                                   'ElectionDistance', 10)
        CommonMethod.update_config(self, 'EconomicModel', 'Staking',
                                   'StakeThreshold', 1000)
        # 启动节点
        self.auto = AutoDeployPlaton()
        self.auto.start_all_node(self.node_yml_path)

    def test_init_token(self):
        '''
        验证链初始化后token各内置账户初始值
        :return:
        '''
        url = CommonMethod.link_list(self)
        platon_ppos = Ppos(url, self.address, self.chainid)
        FOUNDATION = platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.FOUNDATIONADDRESS))
        FOUNDATIONLOCKUP = platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.FOUNDATIONLOCKUPADDRESS))
        STAKING = platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.STAKINGADDRESS))
        INCENTIVEPOOL = platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.INCENTIVEPOOLADDRESS))
        DEVELOPERS = platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.DEVELOPERSADDRESS))
        token_init_total = conf.TOKENTOTAL
        if self.initial_amount['FOUNDATION'] != FOUNDATION:
            log.info("基金会初始金额:{}有误".format(FOUNDATION))
        elif self.initial_amount['FOUNDATIONLOCKUP'] != FOUNDATIONLOCKUP:
            log.info("基金会锁仓初始金额:{}有误".format(FOUNDATIONLOCKUP))
        elif self.initial_amount['STAKING'] != STAKING:
            log.info("质押账户初始金额:{}有误".format(STAKING))
        elif self.initial_amount['INCENTIVEPOOL'] != INCENTIVEPOOL:
            log.info("奖励池初始金额:{}有误".format(INCENTIVEPOOL))
        elif self.initial_amount['DEVELOPERS'] != DEVELOPERS:
            log.info("预留账户初始金额:{}有误".format(DEVELOPERS))
        reality_total = FOUNDATION + FOUNDATIONLOCKUP + STAKING + INCENTIVEPOOL + DEVELOPERS
        assert token_init_total == (
            reality_total), "初始化发行值{}有误".format(reality_total)

    def test_transfer_normal(self):
        '''
        验证初始化之后账户之间转账
        :return:
        '''
        url = CommonMethod.link_list(self)
        platon_ppos = Ppos(url, self.address, self.chainid)
        address1, private_key1 = 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)
        balance = platon_ppos.eth.getBalance(address1)
        log.info("转账金额{}".format(balance))
        assert Web3.toWei(self.value,
                          'ether') == balance, "转账金额:{}失败".format(balance)

    def test_fee_income(self):
        '''
        1、验证初始化之后普通账户转账内置账户
        2、验证初始内置账户没有基金会Staking奖励和出块奖励只有手续费收益
        :return:
        '''
        url = CommonMethod.link_list(self)
        platon_ppos = Ppos(url, self.address, self.chainid)
        incentive_pool_balance_befor = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        log.info('交易前激励池查询余额:{}'.format(incentive_pool_balance_befor))
        address1, private_key1 = 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)
        incentive_pool_balance_after = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        log.info('交易后激励池查询余额:{}'.format(incentive_pool_balance_after))
        difference = incentive_pool_balance_after - incentive_pool_balance_befor
        log.info('手续费的金额:{}'.format(difference))
        assert difference == 1260000000000000000, "手续费{}有误".format(difference)

    def test_punishment_income(self):
        '''
        验证低出块率验证节点的处罚金自动转账到激励池
        :return:
        '''
        url = CommonMethod.link_list(self)
        platon_ppos = Ppos(url, self.address, self.chainid)
        incentive_pool_balance_befor = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        log.info('处罚之前激励池查询余额:{}'.format(incentive_pool_balance_befor))

        #获取节点内置质押节点信息
        nodeId = CommonMethod.read_out_nodeId(self, 'collusion')
        log.info("节点ID:{}".format(nodeId))
        con_node, no_node = get_node_list(self.node_yml_path)
        nodes = con_node + no_node
        for node in nodes:
            if nodeId in node.values():
                node_data = node

        #获取节点质押金额
        punishment_CandidateInfo = platon_ppos.getCandidateInfo(nodeId)
        assert punishment_CandidateInfo['Status'] == True, "查询锁仓信息失败"
        pledge_amount1 = punishment_CandidateInfo['Data']['Released']
        log.info("质押节点质押金:{}".format(pledge_amount1))

        # 停止其中一个正在出块的节点信息
        self.auto = AutoDeployPlaton()
        self.auto.kill(node_data)
        platon_ppos1 = connect_web3(node_data['url'])
        assert not platon_ppos1.isConnected(), "节点:{} 连接异常".format(
            node_data['host'])
        CommonMethod.get_next_settlement_interval(self)
        punishment_CandidateInfo = platon_ppos.getCandidateInfo(nodeId)
        pledge_amount3 = punishment_CandidateInfo['Data']['Released']
        log.info("节点低出块率后节点质押金:{}".format(pledge_amount3))
        incentive_pool_balance_after = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        log.info('处罚之后激励池查询余额:{}'.format(incentive_pool_balance_after))
        assert incentive_pool_balance_after == incentive_pool_balance_befor + (
            pledge_amount1 - pledge_amount3)
class TestLockeDpositionConfig:
    address = conf.ADDRESS
    pwd = conf.PASSWORD
    abi = conf.DPOS_CONTRACT_ABI
    cbft_json_path = conf.CBFT2
    node_yml_path = conf.PPOS_NODE_TEST_YML
    file = conf.CASE_DICT
    privatekey = conf.PRIVATE_KEY
    base_gas_price = 60000000000000
    base_gas = 21000
    staking_gas = base_gas + 32000 + 6000 + 100000
    transfer_gasPrice = Web3.toWei (1, 'ether')
    transfer_gas = 210000000
    value = 1000
    chainid = 120
    ConsensusSize = 150
    time_interval = 10
    initial_amount = {'FOUNDATION': 905000000000000000000000000,
                      'FOUNDATIONLOCKUP': 20000000000000000000000000,
                      'STAKING': 25000000000000000000000000,
                      'INCENTIVEPOOL': 45000000000000000000000000,
                      'DEVELOPERS': 5000000000000000000000000
                      }
    def start_init(self):
        #修改config参数
        CommonMethod.update_config(self,'EconomicModel','Common','ExpectedMinutes',3)
        CommonMethod.update_config(self,'EconomicModel','Common','PerRoundBlocks',10)
        CommonMethod.update_config(self,'EconomicModel','Common','ValidatorCount',5)
        CommonMethod.update_config(self,'EconomicModel','Staking','ElectionDistance',20)
        CommonMethod.update_config(self,'EconomicModel','Staking','StakeThreshold',1000)
        #启动节点
        self.auto = AutoDeployPlaton ()
        self.auto.start_all_node (self.node_yml_path)


    def test_unlock_Normal(self):
        '''
        只有一个锁仓期,到达解锁期返回解锁金额
        :param amount:
        :return:
        '''
        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)
        # 创建锁仓计划
        loukupbalace = Web3.toWei (50, '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'])
        print(address2)
        balance = platon_ppos.eth.getBalance(address2)
        log.info ("锁仓之后账户余额:{}".format (balance))
        log.info("当前块高:{}".format(platon_ppos.eth.blockNumber))
        RestrictingInfo = platon_ppos.GetRestrictingInfo(address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads(RestrictingInfo['Data'])
        assert dict_Info['balance'] == loukupbalace,"锁仓金额:{}有误".format(dict_Info['balance'])

        #验证到达锁仓解锁期之后账户金额
        CommonMethod.get_next_settlement_interval(self)
        balance2 = platon_ppos.eth.getBalance(address2)
        log.info ("解锁之后账户余额:{}".format (balance2))
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        balance = platon_ppos.eth.getBalance(address2)
        assert dict_Info['balance'] == 0, "锁仓金额:{}有误".format (dict_Info['balance'])
        assert balance2 == loukupbalace,"返回的释放锁仓金额:{} 有误".format(balance)

    def test_multiple_unlock_Normal(self):
        '''
        多个锁仓期,到达部分解锁期返回解锁金额
        :return:
        '''
        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)
        loukupbalace = Web3.toWei (50, 'ether')
        plan = [{'Epoch': 1, 'Amount': loukupbalace},{'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'])
        balance = platon_ppos.eth.getBalance (address2)
        log.info ("锁仓之后账户余额:{}".format (balance))
        log.info ("当前块高:{}".format (platon_ppos.eth.blockNumber))
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        assert dict_Info['balance'] == loukupbalace*2, "锁仓金额:{}有误".format (dict_Info['balance'])

        # 验证到达锁仓第一个解锁期之后账户金额
        CommonMethod.get_next_settlement_interval (self)
        balance2 = platon_ppos.eth.getBalance (address2)
        log.info ("到达第一个解锁期后账户余额:{}".format (balance2))
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        balance = platon_ppos.eth.getBalance (address2)
        assert dict_Info['balance'] == loukupbalace, "锁仓金额:{}有误".format (dict_Info['balance'])
        assert dict_Info['Entry'][0]['amount'] == loukupbalace,"第二个解锁期待释放金额:{}".format(dict_Info['Entry'][0]['amount'])
        assert balance2 == loukupbalace, "返回的释放锁仓金额:{} 有误".format (balance)

        #验证到达锁仓第二个解锁期之后账户金额
        CommonMethod.get_next_settlement_interval (self)
        balance3 = platon_ppos.eth.getBalance (address2)
        log.info ("到达第二个解锁期后账户余额:{}".format (balance2))
        RestrictingInfo = platon_ppos.GetRestrictingInfo (address2)
        assert RestrictingInfo['Status'] == True, "获取锁仓计划返回状态为:{} 有误".format (result['Status'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        balance = platon_ppos.eth.getBalance (address2)
        assert dict_Info['balance'] == 0, "锁仓金额:{}有误".format (dict_Info['balance'])
        assert balance3 == loukupbalace*2, "返回的释放锁仓金额:{} 有误".format (balance)

    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'])



    def test_unlock_point_delegtion_notbalance(self):
        '''
        1、到达解锁时间点用锁仓金额去委托节点,到达解锁期账户锁仓不足
        2、到达解锁期账户申请部分赎回委托
        3、全部赎回委托
        :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,
                                                   conf.PRIVATE_KEY)
        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))

        # 申请质押节点
        version = get_version (platon_ppos)
        amount = 100
        result = platon_ppos.createStaking (0, address1, nodeId, 'externalId', 'nodeName', 'website', 'details',
                                            amount, version, privatekey=private_key1, from_address=address1,
                                            gasPrice=self.base_gas_price, gas=self.staking_gas)
        assert result['Status'] == True, "申请质押返回的状态:{},{},用例失败".format (result['Status'], result['ErrMsg'])
        Balance2 = platon_ppos.eth.getBalance (address1)
        log.info ("{}申请质押节点后账户余额:{}".format (address1, Balance2))

        # 创建锁仓计划
        lockupamoutn = 100
        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'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        assert dict_Info['balance'] == loukupbalace, "锁仓计划金额:{}有误".format (dict_Info['balance'])
        assert dict_Info['symbol'] == False, "锁仓的状态:{} 有误".format (dict_Info['symbol'])
        assert dict_Info['debt'] == 0, "欠释放锁仓金额:{} 有误".format (dict_Info['debt'])
        Balance3 = platon_ppos.eth.getBalance (address1)
        log.info ("{}创建锁仓计划后账户余额:{}".format (address1, Balance3))

        # 申请委托验证人节点
        amount = 100
        result = platon_ppos.delegate (1, nodeId, amount, privatekey=private_key2, from_address=address2,
                                              gasPrice=self.base_gas_price, gas=self.staking_gas)
        log.info ("申请委托地址:{}".format (address2))
        assert result['Status'] == True, "申请委托返回的状态:{},用例失败".format (result['Status'])
        balance4 = platon_ppos.eth.getBalance (address2)
        log.info ("{}申请委托验证人节点后账户余额:{}".format (address2,balance4))

        # 到达解锁期之后锁仓账号余额
        CommonMethod.get_next_settlement_interval (self)
        balance5 = platon_ppos.eth.getBalance (address2)
        log.info ("{}到达解锁期后实际账户余额:{}".format (address2,balance5))
        assert balance5 == balance4, "锁仓账户金额:{} 有误".format (balance5)
        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'] == loukupbalace, "欠释放锁仓金额:{} 有误".format (dict_Info['debt'])

        #申请赎回部分委托金
        partamount = 50
        msg = platon_ppos.getCandidateInfo (nodeId)
        stakingBlockNum = msg["Data"]["StakingBlockNum"]
        delegate_info = platon_ppos.unDelegate (stakingBlockNum, nodeId, partamount, privatekey=private_key2,
                                                from_address=address2, gasPrice=self.base_gas_price,
                                                gas=self.staking_gas)
        assert delegate_info['Status'] == True, "申请赎回委托返回的状态:{},用例失败".format (result['Status'])
        balance6 = platon_ppos.eth.getBalance (address2)
        log.info ("{}到达解锁期发起赎回部分委托后账户余额:{}".format (address2,balance6))

        #到达下个解锁期释放部分委托金
        CommonMethod.get_next_settlement_interval (self)
        balance7 = platon_ppos.eth.getBalance (address2)
        log.info ("赎回部分委托金后锁仓账户余额:{}".format (balance7))
        assert balance7 == balance6 + Web3.toWei (partamount, 'ether'),"到达解锁期赎回部分委托后账户余额:{}".format (balance7)
        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'] == loukupbalace - Web3.toWei (partamount, 'ether'), "欠释放锁仓金额:{} 有误".format (dict_Info[                                                                                         'debt'])
        #申请赎回全部委托金
        delegate_info = platon_ppos.unDelegate (stakingBlockNum, nodeId, lockupamoutn - partamount, privatekey=private_key2,
                                                from_address=address2, gasPrice=self.base_gas_price,
                                                gas=self.staking_gas)
        assert delegate_info['Status'] == True, "申请赎回委托返回的状态:{},用例失败".format (result['Status'])
        balance8 = platon_ppos.eth.getBalance (address2)
        log.info ("{}赎回全部委托金后锁仓账户余额:{}".format (address2,balance8))

        #到达下个解锁期释放全部委托金
        CommonMethod.get_next_settlement_interval (self)
        balance9 = platon_ppos.eth.getBalance (address2)
        log.info ("{}赎回全部委托金后实际账户余额:{}".format (address2,balance9))
        log.info ("{}赎回全部委托金后预期账户余额:{}".format (address2,balance8 + Web3.toWei (lockupamoutn - partamount, 'ether')))
        assert balance9 == balance8 + loukupbalace - Web3.toWei (partamount, 'ether'), "到达解锁期赎回委托后账户余额:{}".format                                                                                             (balance9)
        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'] == False, "锁仓的状态:{} 有误".format (dict_Info['symbol'])
        assert dict_Info['debt'] == 0, "欠释放锁仓金额:{} 有误".format (dict_Info['debt'])

    def test_unlock_point_pledge_punish_amount(self):
        '''
        到达解锁期后处罚节点后锁仓账户的余额情况
        :return:
        '''
        nodeId = CommonMethod.get_no_candidate_list (self)
        log.info("质押节点ID:{}".format(nodeId))
        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': 3, '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'])
        Balance2 = platon_ppos.eth.getBalance (address1)
        log.info ("{}发起锁仓后账户余额:{}".format (address1, Balance2))

        # 申请质押节点
        version = get_version (platon_ppos)
        amount = 900
        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'])
        platon_ppos.GetRestrictingInfo(address2)
        result = platon_ppos.getCandidateInfo(nodeId)
        assert result['Status'] == True, "获取质押节点返回状态为:{} 有误".format (result['Status'])
        RestrictingPlan = result['Data']['RestrictingPlanHes']
        assert RestrictingPlan == Web3.toWei (amount, 'ether'),'质押金额:{} 有误'.format(RestrictingPlan)
        Balance3 = platon_ppos.eth.getBalance (address2)
        log.info ("{}质押完锁仓账户余额:{}".format (address2,Balance3))

        #等待验证人加入共识出块节点
        CommonMethod.get_next_settlement_interval (self,1)
        Balance4 = platon_ppos.eth.getBalance (address2)
        log.info ("{}加入共识验证人后账户余额:{}".format (address2, Balance4))

        # 获取节点内置质押节点信息
        con_node, no_node = get_node_list (self.node_yml_path)
        nodes = con_node + no_node
        for node in nodes:
            if nodeId in node.values ():
                node_data = node
        log.info("{}质押节点信息:{}".format(address2,node_data))

        #停止质押节点
        self.auto = AutoDeployPlaton ()
        self.auto.kill (node_data)
        platon_ppos1 = connect_web3 (node_data['url'])
        assert not platon_ppos1.isConnected(),"节点:{} 连接异常".format(node_data['host'])

        # 到达解锁期后处罚节点后锁仓账户
        CommonMethod.get_next_settlement_interval (self,1)
        Balance5 = platon_ppos.eth.getBalance (address2)
        log.info ("{}到达解锁期后处罚节点后预期账户余额:{}".format (address2,Balance4 + Web3.toWei (amount-(amount * 0.2), 'ether')))
        log.info ("{}到达解锁期后处罚节点后实际账户余额:{}".format (address2,Balance5))
        result = platon_ppos.getCandidateInfo(nodeId)
        assert Balance5 == Balance4 + Web3.toWei (amount-(amount * 0.2), '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-(amount * 0.2), 'ether'), "欠释放锁仓金额:{} 有误".format (dict_Info['debt'])


    def test_eliminated_verifier_create_lockup(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 ()

        # 签名转账
        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)

        balance = platon_ppos.eth.getBalance (address1)
        log.info ("{}发起锁仓账户余额:{}".format (address1,balance))

        # 创建锁仓计划
        lockupamoutn = 900
        loukupbalace = Web3.toWei (lockupamoutn, 'ether')
        plan = [{'Epoch': 5, 'Amount': loukupbalace}]
        result = platon_ppos.CreateRestrictingPlan (address1, 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 (address1)
        assert RestrictingInfo['Status'] == True, "查询锁仓计划返回的状态:{},用例失败".format (result['Status'])

        # 申请质押节点
        version = get_version (platon_ppos)
        amount = 200
        result = platon_ppos.createStaking (1, address1, nodeId, 'externalId', 'nodeName', 'website', 'details',
                                            amount, version, privatekey=private_key1, from_address=address1,
                                            gasPrice=self.base_gas_price, gas=self.staking_gas)
        assert result['Status'] == True, "申请质押返回的状态:{},{},用例失败".format (result['Status'], result['ErrMsg'])
        result = platon_ppos.getCandidateInfo(nodeId)
        log.info("质押节点信息:{}".format(result))

        # 等待成为共识验证人
        CommonMethod.get_next_settlement_interval (self)
        CandidateInfo = platon_ppos.getCandidateInfo(nodeId)
        log.info("验证人信息{}".format(CandidateInfo))
        VerifierList = platon_ppos.getVerifierList ()
        log.info ("当前验证人列表:{}".format (VerifierList))
        ValidatorList = platon_ppos.getValidatorList()
        log.info("当前共识验证人列表:{}".format(ValidatorList))



        # for dictinfo in CandidateInfo['Data']:
        #     if nodeId == dictinfo['NodeId']:
        #         log.info("节点id:{}已成为共识验证人".format(nodeId))
        #         break
        #     else:
        #         log.info("节点id:{}未成为共识验证人".format(nodeId))
        #         status=0
        #         assert status == 1

        # 获取节点内置质押节点信息
        con_node, no_node = get_node_list (self.node_yml_path)
        nodes = con_node + no_node
        for node in nodes:
            if nodeId in node.values ():
                node_data = node

        # 停止其中一个正在出块的节点信息
        self.auto = AutoDeployPlaton ()
        self.auto.kill (node_data)
        platon_ppos1 = connect_web3 (node_data['url'])
        assert not platon_ppos1.isConnected (), "节点:{} 连接异常".format (node_data['host'])

        # 等待节点被剔除验证人列表
        CommonMethod.get_next_consensus_wheel (self,2)

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

    def test_owe_amountstack_lock_plan(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)

        # 创建锁仓计划
        lockupamoutn = 500
        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'])

        # 给锁仓账号转手续费
        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(address2)
        log.info("锁仓账户余额:{}".format(balance))

        # 申请质押节点
        version = get_version (platon_ppos)
        amount = 500
        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'])

        #到达解锁期释放锁仓金额
        CommonMethod.get_next_settlement_interval (self)
        platon_ppos.GetRestrictingInfo(address2)
        balance = platon_ppos.eth.getBalance(address2)
        log.info("到达解锁期释放锁仓余额:{}".format(balance))

        # 创建锁仓计划
        lockupamoutn = 100
        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'])
        dict_Info = json.loads (RestrictingInfo['Data'])
        assert dict_Info['balance'] == Web3.toWei (100, 'ether'), "锁仓金额:{}有误".format (dict_Info['balance'])
        assert dict_Info['symbol'] == True, "锁仓的状态:{} 有误".format (dict_Info['symbol'])
        assert dict_Info['debt'] == Web3.toWei (500, 'ether'), "欠释放锁仓金额:{} 有误".format (dict_Info['debt'])



    def testss(self):
        url = CommonMethod.link_list (self)
        platon_ppos = Ppos (url, self.address, self.chainid)
        while 1:
            block = platon_ppos.eth.blockNumber
            print(block)
Beispiel #3
0
class TestDposinit:
    address = Web3.toChecksumAddress(conf.ADDRESS)
    pwd = conf.PASSWORD
    abi = conf.DPOS_CONTRACT_ABI
    cbft_json_path = conf.CBFT2
    node_yml_path = conf.TWENTY_FIVENODE_YML
    file = conf.CASE_DICT
    privatekey = conf.PRIVATE_KEY
    gasPrice = Web3.toWei(0.000000000000000001, 'ether')
    gas = 21000
    value = 1000
    initial_amount = {
        'FOUNDATION': 905000000000000000000000000,
        'FOUNDATIONLOCKUP': 20000000000000000000000000,
        'STAKING': 25000000000000000000000000,
        'INCENTIVEPOOL': 45000000000000000000000000,
        'DEVELOPERS': 5000000000000000000000000
    }

    def ppos_link(self,
                  url=None,
                  address=conf.ADDRESS,
                  privatekey=conf.PRIVATE_KEY):
        if url is None:
            node_info = get_node_info(conf.TWENTY_FIVENODE_YML)
            self.rpc_list, enode_list, nodeid_list, ip_list, port_list = node_info.get(
                'collusion')
            rpc_list_length = len(self.rpc_list) - 1
            index = random.randint(0, rpc_list_length)
            url = self.rpc_list[index]

        self.platon_ppos = Ppos(url,
                                address=address,
                                chainid=101,
                                privatekey=privatekey)
        return self.platon_ppos

    def create_new_address(self, platon_ppos):
        self.new_address = platon_ppos.Web3.toChecksumAddress(
            self.ppos_link().web3.personal.newAccount(self.pwd))
        return self.new_address

    # def update_config(self, file, key, data):
    #     with open(self.file, 'r', encoding='utf-8') as f:
    #         res = json.loads(f.read())

    def ppos_sendTransaction(self, to_address, from_address, gas, gasPrice,
                             value):
        platon_ppos = self.ppos_link()
        self.send_data = {
            "to": to_address,
            "from": from_address,
            "gas": gas,
            "gasPrice": gasPrice,
            "value": value,
        }
        a = platon_ppos.eth.estimateGas(self.send_data)
        tx_hash = platon_ppos.eth.sendTransaction(self.send_data)
        self.platon_ppos.eth.waitForTransactionReceipt(tx_hash)

    def setup_class(self):
        self.auto = AutoDeployPlaton(cbft=self.cbft_json_path)
        self.auto.start_all_node(self.node_yml_path)

    def test_init_token(self):
        '''
        验证链初始化后token各内置账户初始值
        :return:
        '''

        platon_ppos = self.ppos_link()
        FOUNDATION = platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.FOUNDATIONADDRESS))
        assert self.initial_amount['FOUNDATION'] == FOUNDATION
        FOUNDATIONLOCKUP = self.platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.FOUNDATIONLOCKUPADDRESS))
        assert self.initial_amount['FOUNDATIONLOCKUP'] == FOUNDATIONLOCKUP
        STAKING = self.platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.STAKINGADDRESS))
        assert self.initial_amount['STAKING'] == STAKING
        INCENTIVEPOOL = self.platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.INCENTIVEPOOLADDRESS))
        assert self.initial_amount['INCENTIVEPOOL'] == INCENTIVEPOOL
        DEVELOPERS = self.platon_ppos.eth.getBalance(
            Web3.toChecksumAddress(conf.DEVELOPERSADDRESS))
        assert self.initial_amount['DEVELOPERS'] == DEVELOPERS
        token_init_total = conf.TOKENTOTAL
        assert token_init_total == (FOUNDATION + FOUNDATIONLOCKUP + STAKING +
                                    INCENTIVEPOOL + DEVELOPERS)

    def test_transfer_normal(self):
        '''
        验证初始化之后账户之间转账
        :return:
        '''
        platon_ppos = self.ppos_link()
        address1 = '0x684b43Cf53C78aA567840174a55442d7a9282679'
        privatekey1 = 'a5ac52e828e2656309933339cf12d30755f918e368fffc7c265b55da718ff893'
        try:
            platon_ppos.send_raw_transaction(
                '', Web3.toChecksumAddress(conf.ADDRESS),
                Web3.toChecksumAddress(address1), self.gasPrice, self.gas,
                self.value, conf.PRIVATE_KEY)
            Balance1 = platon_ppos.eth.getBalance('address1')
            assert Web3.toWei(self.value, 'ether') == Balance1
        except:
            status = 1
            assert status == 0, '账号余额不足,无法发起转账'

    def test_transfer_notsufficientfunds(self):
        '''
        账户余额不足的情况下进行转账
        :return:
        '''
        platon_ppos = self.ppos_link()
        #账户1
        address1 = '0x684b43Cf53C78aA567840174a55442d7a9282679'
        privatekey1 = 'a5ac52e828e2656309933339cf12d30755f918e368fffc7c265b55da718ff893'
        #账户2
        address2 = '0xc128fDBb500096974Db713b563dBeF597461C5dD'
        privatekey2 = 'c03975513e3de5b1c63fb4b31470111119d5ef1e580d14ebd23aee48f580ac13'
        balance = platon_ppos.eth.getBalance(address1)
        if balance == 0:
            try:
                platon_ppos.send_raw_transaction(
                    '', Web3.toChecksumAddress(address1),
                    Web3.toChecksumAddress(address2), self.gasPrice, self.gas,
                    self.value, privatekey1)
            except:
                status = 1
                assert status == 0, '账号余额不足,无法发起转账'
        else:
            log.info('账号:{}已转账,请切换账号再试'.format(address1))

    def test_transfer_funds(self):
        '''
        验证初始化之后普通账户转账内置账户
        :return:
        '''
        platon_ppos = self.ppos_link()
        lockup_balancebe_before = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        # 签名转账
        try:
            platon_ppos.send_raw_transaction(
                '', Web3.toChecksumAddress(conf.ADDRESS),
                Web3.toChecksumAddress(conf.INCENTIVEPOOLADDRESS),
                self.gasPrice, self.gas, self.value, conf.PRIVATE_KEY)
            lockup_balancebe_after = platon_ppos.eth.getBalance(
                conf.INCENTIVEPOOLADDRESS)
            assert lockup_balancebe_before + Web3.toWei(
                self.value, 'ether') == lockup_balancebe_after
        except:
            status = 1
            assert status == 0, '无法发起转账'

    @allure.title("查看初始化时锁仓余额和锁仓信息")
    def test_token_loukup(self):
        '''
        查询初始化链后基金会锁仓金额
        以及查询初始锁仓计划信息的有效性
        :return:
        '''
        #platon_ppos = self.ppos_link ()
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        lockupbalance = platon_ppos.eth.getBalance(
            conf.FOUNDATIONLOCKUPADDRESS)
        FOUNDATIONLOCKUP = self.initial_amount['FOUNDATIONLOCKUP']
        assert lockupbalance == FOUNDATIONLOCKUP
        result = platon_ppos.GetRestrictingInfo(conf.INCENTIVEPOOLADDRESS)
        print(result)
        if result['Status'] == 'True':
            assert result['Date']['balance'] == self.initial_amount[
                'FOUNDATIONLOCKUP']
        else:
            log.info("初始锁仓金额:{},锁仓计划信息查询结果有误".format(lockupbalance))

    def test_loukupplan(self):
        '''
        验证正常锁仓功能
        参数输入:
        Epoch:1
        Amount:50 ether
        :return:
        '''
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        #platon_ppos = self.ppos_link ()
        address1 = '0x472599739f398c24ad8Cdc03476b20D6469eAf46'
        privatekey1 = '61279a4b654aef7c3065c0cf550cdce460682875c218de893544a4799b57cc41'
        #非签名转账
        # platon_ppos.web3.personal.unlockAccount(conf.ADDRESS, conf.PASSWORD, 2222)
        # self.ppos_sendTransaction(address1,conf.ADDRESS,self.gas,self.gasPrice,Web3.toWei(10000,'ether'))
        #签名转账
        platon_ppos.send_raw_transaction('',
                                         Web3.toChecksumAddress(conf.ADDRESS),
                                         Web3.toChecksumAddress(address1),
                                         self.gasPrice, self.gas, self.value,
                                         conf.PRIVATE_KEY)
        balance = platon_ppos.eth.getBalance(address1)
        log.info("发起锁仓账户的余额:{}", balance)
        if balance > 0:
            try:
                loukupbalace = Web3.toWei(50, 'ether')
                plan = [{'Epoch': 1, 'Amount': loukupbalace}]
                lockup_before = platon_ppos.eth.getBalance(
                    conf.FOUNDATIONLOCKUPADDRESS)
                #创建锁仓计划
                result = platon_ppos.CreateRestrictingPlan(
                    address1,
                    plan,
                    privatekey1,
                    from_address=address1,
                    gasPrice=self.gasPrice)
                lockup_after = platon_ppos.eth.getBalance(
                    conf.FOUNDATIONLOCKUPADDRESS)
                #查看锁仓计划明细
                RestrictingInfo = platon_ppos.GetRestrictingInfo(address1)
                if result['status'] == 'True':
                    assert RestrictingInfo['balance'] == loukupbalace
                else:
                    log.info("查询锁仓计划信息返回状态为:{}".format(result['status']))

                assert lockup_after == lockup_before + loukupbalace
            except:
                status = 1
                assert status == 0, '创建锁仓计划失败'

    @allure.title("根据不同参数创建锁仓计划")
    @pytest.mark.parametrize('number,amount', [(1, 0.1), (-1, 3), (0.1, 3),
                                               (37, 3)])
    def test_loukupplan_abnormal(self, number, amount):
        '''
        创建锁仓计划时,参数有效性验证
        number : 锁仓解锁期
        amount : 锁仓金额
        :param number:
        :param amount:
        :return:
        '''
        address1 = '0x9148528b98a0065D185F01dbc59baB88CdbE7Ad2'
        private_key1 = '6ccbf153f7409af1e5df7a1ef77daaca4759f0a6b50ef73fe9ccd5738cc2fda1'
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        # platon_ppos = self.ppos_link ()
        try:
            loukupbalace = Web3.toWei(amount, 'ether')
            plan = [{'Epoch': number, 'Amount': loukupbalace}]
            #当锁仓金额输入小于 1ether
            if number >= 1 and amount < 1:
                result = platon_ppos.CreateRestrictingPlan(
                    address1,
                    plan,
                    conf.PRIVATE_KEY,
                    from_address=conf.ADDRESS,
                    gasPrice=self.gasPrice,
                    gas=self.gas)
                assert result['status'] == 'false'
            #当锁仓解锁期输入非正整数倍
            elif number < 0 or type(number) == float:
                result = platon_ppos.CreateRestrictingPlan(
                    address1,
                    plan,
                    conf.PRIVATE_KEY,
                    from_address=conf.ADDRESS,
                    gasPrice=self.gasPrice,
                    gas=self.gas)
                assert result['status'] == 'false'
            #当锁仓解锁期大于36个结算期
            elif number > 36:
                result = platon_ppos.CreateRestrictingPlan(
                    address1,
                    plan,
                    conf.PRIVATE_KEY,
                    from_address=conf.ADDRESS,
                    gasPrice=self.gasPrice,
                    gas=self.gas)
                assert result['status'] == 'false'

        except:
            status = 1
            assert status == 0, '创建锁仓计划失败'

    @allure.title("锁仓金额大于账户余额")
    def test_loukupplan_amount(self):
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        # platon_ppos = self.ppos_link ()
        address1 = '0x51a9A03153a5c3c203F6D16233e3B7244844A457'
        privatekey1 = '25f9fdf3249bb47f239df0c59d23781c271e8b7e9a94e9e694c15717c1941502'
        # 签名转账
        platon_ppos.send_raw_transaction('',
                                         Web3.toChecksumAddress(conf.ADDRESS),
                                         Web3.toChecksumAddress(address1),
                                         self.gasPrice, self.gas, self.value,
                                         conf.PRIVATE_KEY)
        balance = platon_ppos.eth.getBalance(address1)
        if balance > 0:
            try:
                loukupbalace = Web3.toWei(10000, 'ether')
                plan = [{'Epoch': 1, 'Amount': loukupbalace}]
                result = platon_ppos.CreateRestrictingPlan(
                    address1,
                    plan,
                    privatekey1,
                    from_address=address1,
                    gasPrice=self.gasPrice,
                    gas=self.gas)
                assert result['status'] == 'false'
            except:
                status = 1
                assert status == 0, '创建锁仓计划失败'
        else:
            log.info('error:转账失败')

    @allure.title("多个锁仓期金额情况验证")
    @pytest.mark.parametrize('balace1,balace2', [(300, 300), (500, 600)])
    def test_loukupplan_Moredeadline(self, balace1, balace2):
        '''
        验证一个锁仓计划里有多个解锁期
        amount : 锁仓
        balace1 : 第一个锁仓期的锁仓金额
        balace2 : 第二个锁仓期的锁仓金额
        :return:
        '''
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        # platon_ppos = self.ppos_link ()
        address1 = '0x51a9A03153a5c3c203F6D16233e3B7244844A457'
        privatekey1 = '25f9fdf3249bb47f239df0c59d23781c271e8b7e9a94e9e694c15717c1941502'
        # 签名转账
        platon_ppos.send_raw_transaction('',
                                         Web3.toChecksumAddress(conf.ADDRESS),
                                         Web3.toChecksumAddress(address1),
                                         self.gasPrice, self.gas, self.value,
                                         conf.PRIVATE_KEY)
        balance = platon_ppos.eth.getBalance(address1)
        if balance > 0:
            try:
                loukupbalace1 = Web3.toWei(balace1, 'ether')
                loukupbalace2 = Web3.toWei(balace2, 'ether')
                if (loukupbalace1 + loukupbalace2) < self.value:
                    plan = [{
                        'Epoch': 1,
                        'Amount': loukupbalace1
                    }, {
                        'Epoch': 2,
                        'Amount': loukupbalace2
                    }]
                    result = platon_ppos.CreateRestrictingPlan(
                        address1,
                        plan,
                        privatekey1,
                        from_address=address1,
                        gasPrice=self.gasPrice,
                        gas=self.gas)
                    assert result['status'] == 'True'
                elif self.value <= (loukupbalace1 + loukupbalace2):
                    plan = [{
                        'Epoch': 1,
                        'Amount': loukupbalace1
                    }, {
                        'Epoch': 2,
                        'Amount': loukupbalace2
                    }]
                    result = platon_ppos.CreateRestrictingPlan(
                        address1,
                        plan,
                        privatekey1,
                        from_address=address1,
                        gasPrice=self.gasPrice,
                        gas=self.gas)
                    assert result['status'] == 'false'
                else:
                    log.info('锁仓输入的金额:{}出现异常'.format(
                        (loukupbalace1 + loukupbalace2)))

            except:
                status = 1
                assert status == 0, '创建锁仓计划失败'
        else:
            log.info('error:转账失败')

    @allure.title("锁仓计划多个相同解锁期情况验证")
    @pytest.mark.parametrize('code,', [(1), (2)])
    def test_loukupplan_sameperiod(self, code):
        '''
        验证一个锁仓计划里有多个相同解锁期
        code =1 :同个account在一个锁仓计划里有相同解锁期
        code =2 :同个account在不同锁仓计划里有相同解锁期
        :return:
        '''
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        # platon_ppos = self.ppos_link ()
        address1 = '0x51a9A03153a5c3c203F6D16233e3B7244844A457'
        privatekey1 = '25f9fdf3249bb47f239df0c59d23781c271e8b7e9a94e9e694c15717c1941502'
        # 签名转账
        platon_ppos.send_raw_transaction('',
                                         Web3.toChecksumAddress(conf.ADDRESS),
                                         Web3.toChecksumAddress(address1),
                                         self.gasPrice, self.gas, 1000,
                                         conf.PRIVATE_KEY)
        balance = platon_ppos.eth.getBalance(address1)
        if balance > 0:
            try:
                period1 = 1
                period2 = 2
                loukupbalace = Web3.toWei(100, 'ether')
                if code == 1:
                    plan = [{
                        'Epoch': period1,
                        'Amount': loukupbalace
                    }, {
                        'Epoch': period1,
                        'Amount': loukupbalace
                    }]
                    result = platon_ppos.CreateRestrictingPlan(
                        address1,
                        plan,
                        privatekey1,
                        from_address=address1,
                        gasPrice=self.gasPrice,
                        gas=self.gas)
                    assert result['status'] == 'True'
                    RestrictingInfo = platon_ppos.GetRestrictingInfo(address1)
                    json_data = json.loads(RestrictingInfo['Data'])
                    assert json_data['Entry'][0]['amount'] == (loukupbalace +
                                                               loukupbalace)

                elif code == 2:
                    plan = [{
                        'Epoch': period1,
                        'Amount': loukupbalace
                    }, {
                        'Epoch': period2,
                        'Amount': loukupbalace
                    }]
                    result = platon_ppos.CreateRestrictingPlan(
                        address1,
                        plan,
                        privatekey1,
                        from_address=address1,
                        gasPrice=self.gasPrice,
                        gas=self.gas)
                    assert result['status'] == 'True'
                    loukupbalace2 = Web3.toWei(200, 'ether')
                    plan = [{
                        'Epoch': period1,
                        'Amount': loukupbalace2
                    }, {
                        'Epoch': period2,
                        'Amount': loukupbalace2
                    }]
                    result1 = platon_ppos.CreateRestrictingPlan(
                        address1,
                        plan,
                        privatekey1,
                        from_address=address1,
                        gasPrice=self.gasPrice,
                        gas=self.gas)
                    assert result1['status'] == 'True'
                    RestrictingInfo = platon_ppos.GetRestrictingInfo(address1)
                    json_data = json.loads(RestrictingInfo['Data'])
                    assert json_data['Entry'][0]['amount'] == (loukupbalace +
                                                               loukupbalace2)
                else:
                    log.info('输入的code:{}有误'.format(code))
            except:
                status = 1
                assert status == 0, '创建锁仓计划失败'
        else:
            log.info('error:转账失败')

    # def test_loukupplan_amount(self):
    #     '''
    #     账户余额为0的时候调用锁仓接口
    #     :return:
    #     '''
    #     platon_ppos = Ppos ('http://10.10.8.157:6789', self.address, chainid=102,
    #                         privatekey=conf.PRIVATE_KEY)
    #     # platon_ppos = self.ppos_link ()
    #     address1 = '0x51a9A03153a5c3c203F6D16233e3B7244844A457'
    #     privatekey1 = '25f9fdf3249bb47f239df0c59d23781c271e8b7e9a94e9e694c15717c1941502'
    #     try:
    #         loukupbalace = Web3.toWei (100, 'ether')
    #         plan = [{'Epoch': 1, 'Amount': loukupbalace}]
    #         result = platon_ppos.CreateRestrictingPlan (address1, plan, privatekey1,
    #                                                     from_address=address1, gasPrice=self.gasPrice, gas=self.gas)
    #         assert result['status'] == 'false'
    #     except:
    #         status = 1
    #         assert status == 0, '创建锁仓计划失败'

    def test_Incentive_pool(self):
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        #platon_ppos = self.ppos_link ()
        lockupbalance = platon_ppos.eth.getBalance(conf.INCENTIVEPOOLADDRESS)
        log.info('激励池查询余额:{}'.format(lockupbalance))
        INCENTIVEPOOL = self.initial_amount['INCENTIVEPOOL']
        assert lockupbalance == INCENTIVEPOOL

    def test_fee_income(self):
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        #platon_ppos = self.ppos_link ()
        incentive_pool_balance_befor = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        log.info('交易前激励池查询余额:{}'.format(incentive_pool_balance_befor))
        # 签名转账
        address1 = '0x51a9A03153a5c3c203F6D16233e3B7244844A457'
        privatekey1 = '25f9fdf3249bb47f239df0c59d23781c271e8b7e9a94e9e694c15717c1941502'
        try:
            platon_ppos.send_raw_transaction(
                '', Web3.toChecksumAddress(conf.ADDRESS),
                Web3.toChecksumAddress(address1), self.gasPrice, self.gas,
                self.value, conf.PRIVATE_KEY)
            balance = platon_ppos.eth.getBalance(address1)
            if balance == Web3.toWei(self.value, 'ether'):
                incentive_pool_balance_after = platon_ppos.eth.getBalance(
                    conf.INCENTIVEPOOLADDRESS)
                log.info('交易前激励池查询余额:{}'.format(incentive_pool_balance_after))
                difference = incentive_pool_balance_after - incentive_pool_balance_befor
                log.info('手续费的金额:{}'.format(difference))
                assert difference == self.gas
            else:
                log.info("转账{}金额错误".format(Web3.toWei(self.value, 'ether')))
        except:
            status = 1
            assert status == 0, '转账失败'

    def test_punishment_income(self):
        '''
        验证低出块率验证节点的处罚金自动转账到激励池
        :return:
        '''
        #随机获取其中一个正在出块的节点信息
        node_info = get_node_list(conf.TWENTY_FIVENODE_YML)
        node_info_length = len(node_info) - 1
        index = random.randint(0, node_info_length)
        node_data = node_info[0][index]
        print(node_data)
        platon_ppos = Ppos('http://10.10.8.157:6789',
                           self.address,
                           chainid=102,
                           privatekey=conf.PRIVATE_KEY)
        # platon_ppos = self.ppos_link ()
        incentive_pool_balance_befor = platon_ppos.eth.getBalance(
            conf.INCENTIVEPOOLADDRESS)
        log.info('处罚之前激励池查询余额:{}'.format(incentive_pool_balance_befor))
        #停止其中一个正在出块的节点信息
        try:
            self.auto = AutoDeployPlaton(cbft=self.cbft_json_path)
            self.auto.kill(node_data)
            platon_ppos = self.ppos_link(node_data['url'])
            if not platon_ppos.web3.isConnected():
                platon_ppos = self.ppos_link()
                current_block = platon_ppos.eth.blockNumber()
                waiting_time = 250 - (current_block % 250)
                time.sleep(waiting_time + 1)
                incentive_pool_balance_after = platon_ppos.eth.getBalance(
                    conf.INCENTIVEPOOLADDRESS)
                log.info('处罚之后激励池查询余额:{}'.format(incentive_pool_balance_after))
                punishment_CandidateInfo = platon_ppos.getCandidateInfo(
                    node_data['id'])
                if punishment_CandidateInfo['Status'] == 'True':
                    punishment_amount = punishment_CandidateInfo['Data'][
                        'Shares'] * (20 / 100)
                    assert incentive_pool_balance_after == incentive_pool_balance_befor + punishment_amount
                else:
                    log.info("查询处罚节点:{}质押信息失败".format(node_data['host']))
            else:
                log.info("当前质押节点:{}链接正常".format(node_data['host']))
        except:
            status = 1
            assert status == 0, '停止节点:{}失败'.format(node_data['host'])