Exemplo n.º 1
0
    def wait_for_confirmation(self, tx_hash, max_retries=None, polling_interval=None):
        """
        Wait for a transaction to be confirmed by at least "key_block_confirmation_num" blocks (default 3)
        The amount of blocks can be configured in the Config object using key_block_confirmation_num parameter

        :param tx_hash: the hash of the transaction to wait for
        :param max_retries: the maximum number of retries to test for transaction
        :param polling_interval: the interval between transaction polls
        :return: the block height of the transaction if it has been found

        """
        # first wait for the transaction to be found
        tx_height = self.wait_for_transaction(tx_hash)
        # now calculate the min block height
        min_block_height = tx_height + self.config.key_block_confirmation_num
        # get teh
        retries = max_retries if max_retries is not None else self.config.poll_block_max_retries
        interval = polling_interval if polling_interval is not None else self.config.poll_block_retries_interval
        if retries <= 0 or interval <= 0:
            raise ValueError("max_retries and polling_interval must be greater than 0")
        # start polling
        n = 1
        total_sleep = 0
        while True:
            current_height = self.get_current_key_block_height()
            # if the tx.block_height >= min_block_height we are ok
            if current_height >= min_block_height:
                break
            if n >= retries:
                raise TransactionWaitTimeoutExpired(tx_hash=tx_hash, reason=f"The transaction was not included in {total_sleep} seconds, wait aborted")
            # calculate sleep time
            time.sleep(interval)
            total_sleep += interval
            # increment n
            n += 1
        return tx_height