def run_wrong_addr_case(acc: Account, with_draw_times=1):
    global output, fee, value, balance
    balances_start = get_mainchain_balances([output])
    for i in range(with_draw_times):
        log_print(
            "=================Withdraw for {} time===================".format(
                i))
        main_addr = output
        if i == 1:
            log_print(
                "This withdraw is use wrong mainchain address: {}".format(
                    output_wrong))
            main_addr = output_wrong
        txhash, err = get_receipt_hash(main_addr, balance, fee, value, acc)
        if not txhash:
            log_print_is_case_pass(False, case_name,
                                   "Call contract methon Fail!" + err)
            return

    log_print(
        "To make sure the address of main chain has received the money, We need to wait main chain mining 151 blocks! "
    )
    start_height = rpc.get_block_count()

    while True:
        current_height = rpc.get_block_count()
        Logger.info("current height: {}, dest height:{} ".format(
            current_height, (start_height + 151)))
        if current_height - start_height > 150:
            break

        # 调用主链出块,要求时间等待3秒,给主链共识
        rpc.discrete_mining(1)
        time.sleep(3)

    balances_end = get_mainchain_balances([output])
    addr = output
    if with_draw_times > 1:
        with_draw_times -= 1
    value_term = balance / 1e18 * with_draw_times
    fee_term = fee / 1e18 * with_draw_times
    if float(balances_start[addr]) + value_term - fee_term - float(
            balances_end[addr]) > 0.0:
        infostr = "address: " + addr + "balance_start: " + str(
            balances_start[addr]) + "balance_end: " + str(
                balances_end[addr]) + "withdraw_value: " + str(
                    value_term) + "fee: " + str(fee_term)
        log_print_is_case_pass(
            False, "wrong_addr_case",
            "Withdraw Fail in balance calc! detail:" + infostr)
        return
    log_print_is_case_pass(True, "wrong_addr_case")
    def mining_side_blocks(self, side_port: int, num=1):
        side_height_begin = rpc.get_block_count(side_port)
        while True:
            main_height = rpc.get_block_count()
            side_height = rpc.get_block_count(side_port)

            Logger.debug("{} main height: {}, side height: {}".format(
                self.tag, main_height, side_height))

            if side_height - side_height_begin > num:
                break

            rpc.discrete_mining(1)
            time.sleep(3)
    def register_producers(self, start: int, end: int, without_mining=False):
        for i in range(start, end):
            ela_node = self.node_manager.ela_nodes[i]
            ret = self.register_producer(ela_node)
            if not ret:
                return False

            current_height = rpc.get_block_count()
            last_height = current_height
            while True:
                rpc.discrete_mining(1)
                current_height = rpc.get_block_count()
                Logger.debug("{} current height: {}".format(self.tag, current_height))
                if current_height - 6 > last_height:
                    break
                time.sleep(1)

            Logger.info("{} register node-{} to be a producer on success!\n".format(self.tag, i))
        return True
    def check_nodes_height(self):
        Logger.debug(
            "{} check the all nodes whether have the same height".format(
                self.tag))
        time.sleep(3)
        heights = list()

        for node in self.node_manager.ela_nodes:
            if not node.running:
                continue

            height = rpc.get_block_count(node.rpc_port)
            Logger.debug("{} node{} height\t{}".format(self.tag, node.index,
                                                       height))
            heights.append(height)

        global h0
        h0 = heights[0]

        for h in heights[1:]:
            if h != h0:
                return False

        return True
    def cross_chain_transaction(self, side_node_type: str, recharge: bool, target_address=""):
        if side_node_type is None or side_node_type is "":
            return False

        global cross_address
        global side_port
        global result
        global balance_port
        global cross_input_key

        if side_node_type is "did":
            side_port = 10136
            cross_did_account = self.node_manager.keystore_manager.cross_did_account
            cross_address = cross_did_account.address()
            cross_input_key = cross_did_account.private_key()

        elif side_node_type is "token":
            side_port = 10146
            cross_token_account = self.node_manager.keystore_manager.cross_token_account
            cross_address = cross_token_account.address()
            cross_input_key = cross_token_account.private_key()

        elif side_node_type is "neo":
            side_port = 10156
            cross_neo_account = self.node_manager.keystore_manager.cross_neo_account
            if target_address == "":
                cross_address = cross_neo_account.address()
            else:
                cross_address = target_address
            cross_input_key = cross_neo_account.private_key()

        if recharge:
            port = self.rpc_port
            balance_port = side_port
            input_private_key = self.tap_account.private_key()
            lock_address = self.params.arbiter_params.side_info[side_node_type][constant.SIDE_RECHARGE_ADDRESS]
            amount = 1000 * constant.TO_SELA

        else:
            port = side_port
            balance_port = self.rpc_port
            input_private_key = cross_input_key
            lock_address = self.params.arbiter_params.side_info[side_node_type][constant.SIDE_WITHDRAW_ADDRESS]
            cross_address = self.tap_account.address()
            amount = 300 * constant.TO_SELA

        balance1 = rpc.get_balance_by_address(cross_address, balance_port)

        ret = self.transfer_cross_chain_asset(
            input_private_key=input_private_key,
            lock_address=lock_address,
            cross_address=cross_address,
            amount=amount,
            recharge=recharge,
            port=port
        )

        if not ret:
            Logger.error("{} transfer cross chain asset failed".format(self.tag))
            return False

        side_height_begin = rpc.get_block_count(side_port)
        while True:
            main_height = rpc.get_block_count()
            side_height = rpc.get_block_count(side_port)

            Logger.debug("{} main height: {}, side height: {}".format(self.tag, main_height, side_height))

            if side_height - side_height_begin > 10:
                break

            rpc.discrete_mining(1)
            time.sleep(3)

        balance2 = rpc.get_balance_by_address(cross_address, balance_port)
        Logger.debug("{} recharge balance1: {}".format(self.tag, balance1))
        Logger.debug("{} recharge balance2: {}".format(self.tag, balance2))

        if isinstance(balance1, dict):
            before_balance = list(balance1.values())[0]
        else:
            before_balance = balance1

        if isinstance(balance2, dict):
            after_balance = list(balance2.values())[0]
        else:
            after_balance = balance2

        result = (float(after_balance) - float(before_balance)) * constant.TO_SELA > float(amount - 3 * 10000)
        Logger.debug("{} recharge result: {}".format(self.tag, result))

        return result
 def get_current_height():
     return rpc.get_block_count() - 1
 def get_current_height():
     height = rpc.get_block_count() - 1
     Logger.info("current height: {}".format(height))
     return height
    def cross_chain_transaction(self, side_node_type: str, recharge: bool, input_account=None):
        if side_node_type is None or side_node_type is "":
            return False, None, None, None, None

        global cross_key_account
        global side_port
        global result
        global balance_port

        if not input_account:
            input_account = self.tap_account

        if side_node_type is "did":
            side_port = 10136
            cross_key_account = self.node_manager.keystore_manager.cross_did_account
        elif side_node_type is "token":
            side_port = 10146
            cross_key_account = self.node_manager.keystore_manager.cross_token_account
        elif side_node_type is "neo":
            side_port = 10156
            cross_key_account = self.node_manager.keystore_manager.cross_neo_account
        elif side_node_type is "geth":
            side_port = 1111
            from src.core.nodes.eth import Eth
            cross_key_account = Eth.get_random_eth_account()

        if recharge:
            port = self.rpc_port
            balance_port = side_port
            input_private_key = input_account.private_key()
            lock_address = self.params.arbiter_params.side_info[side_node_type][constant.SIDE_RECHARGE_ADDRESS]
            cross_address = cross_key_account.address()
            amount = 100 * constant.TO_SELA

        else:
            port = side_port
            balance_port = self.rpc_port
            input_private_key = cross_key_account.private_key()
            lock_address = self.params.arbiter_params.side_info[side_node_type][constant.SIDE_WITHDRAW_ADDRESS]
            cross_address = self.tap_account.address()
            amount = 100 * constant.TO_SELA

        cross_address = cross_address[2:]
        Logger.warn("{} cross address: {}".format(self.tag, cross_address))

        balance1 = rpc.get_balance_by_address(cross_address, balance_port, side_node_type)

        ret, tx_hash = self.transfer_cross_chain_asset(
            input_private_key=input_private_key,
            lock_address=lock_address,
            cross_address=cross_address,
            amount=amount,
            recharge=recharge,
            port=port
        )

        if not ret:
            Logger.error("{} transfer cross chain asset failed".format(self.tag))
            return False, None, None, None, None

        side_height_begin = rpc.get_block_count(side_port)
        main_height = rpc.get_block_count()
        Logger.info(
            "To make sure recharge tx be putted into main chain block, we need to wait main chain mining 16 blocks!")
        while True:
            current_height = rpc.get_block_count()
            side_height = rpc.get_block_count(side_port)

            Logger.info("{} Current height: {}, dest height: {}".format(self.tag, current_height, (main_height + 16)))

            if side_node_type == "geth":
                if current_height - main_height > 15:
                    break
            else:
                if side_height - side_height_begin > 10:
                    break
            rpc.discrete_mining(1)
            time.sleep(3)

        balance2 = rpc.get_balance_by_address(cross_address, balance_port, side_node_type)
        Logger.debug("{} recharge balance1: {}".format(self.tag, balance1))
        Logger.debug("{} recharge balance2: {}".format(self.tag, balance2))

        # result = (float(balance2) - float(balance1)) * constant.TO_SELA > float(amount - 3 * 10000)
        # Logger.debug("{} recharge result: {}".format(self.tag, result))
        #
        return True, tx_hash, cross_address, amount, TX_FEE
Esempio n. 9
0
 def get_current_height(self):
     height = rpc.get_block_count(port=self.rpc_port) - 1
     Logger.info("{} current height: {}".format(self.tag, height))
     return height