示例#1
0
    def from_bytes(b):
        """ Deserializes a byte stream into a WalletTransaction.

        Args:
            b (bytes): byte stream starting with the version.

        Returns:
            tuple: First element of the tuple is the WalletTransaction,
                   second is the remainder of the byte stream.
        """
        t, b1 = Transaction.from_bytes()
        return WalletTransaction.from_transaction(t), b1
示例#2
0
    def from_bytes(b):
        """ Deserializes a byte stream into a WalletTransaction.

        Args:
            b (bytes): byte stream starting with the version.

        Returns:
            tuple: First element of the tuple is the WalletTransaction,
                   second is the remainder of the byte stream.
        """
        t, b1 = Transaction.from_bytes()
        return WalletTransaction.from_transaction(t), b1
示例#3
0
def mine_work(work_msg, enonce1, enonce2_size):
    """ Mine the work using a CPU to find a valid solution.

    Loop until the CPU finds a valid solution of the given work.

    Todo:
        Slow down the click echo when on a 21BC.

    Args:
        work_msg (WorkNotification): the work given by the pool API
        enonce1 (bytes): extra nonce required to make the coinbase transaction
        enonce2_size (int): size of the extra nonce 2 in bytes
    """
    pool_target = utils.bits_to_target(work_msg.bits_pool)
    for enonce2_num in range(0, 2**(enonce2_size * 8)):
        enonce2 = enonce2_num.to_bytes(enonce2_size, byteorder="big")

        cb_txn, _ = Transaction.from_bytes(work_msg.coinb1 + enonce1 +
                                           enonce2 + work_msg.coinb2)
        cb = CompactBlock(
            work_msg.height,
            work_msg.version,
            Hash(work_msg.prev_block_hash),
            work_msg.ntime,
            work_msg.nbits,  # lower difficulty work for testing
            work_msg.merkle_edge,
            cb_txn)

        row_counter = 0
        for nonce in range(0xffffffff):

            if nonce % 6e3 == 0:
                logger.info(click.style(u'█', fg='green'), nl=False)
                row_counter += 1
            if row_counter > 40:
                row_counter = 0
                logger.info("")

            cb.block_header.nonce = nonce
            h = cb.block_header.hash.to_int('little')
            if h < pool_target:
                share = Share(enonce2=enonce2,
                              nonce=nonce,
                              work_id=work_msg.work_id,
                              otime=int(time.time()))
                # adds a new line at the end of progress bar
                logger.info("")
                return share

        logger.info("Exhausted enonce1 space. Changing enonce2")
示例#4
0
def mine_work(work_msg, enonce1, enonce2_size):
    """ Mine the work using a CPU to find a valid solution.

    Loop until the CPU finds a valid solution of the given work.

    Todo:
        Slow down the click echo when on a 21BC.

    Args:
        work_msg (WorkNotification): the work given by the pool API
        enonce1 (bytes): extra nonce required to make the coinbase transaction
        enonce2_size (int): size of the extra nonce 2 in bytes
    """
    pool_target = utils.bits_to_target(work_msg.bits_pool)
    for enonce2_num in range(0, 2 ** (enonce2_size * 8)):
        enonce2 = enonce2_num.to_bytes(enonce2_size, byteorder="big")

        cb_txn, _ = Transaction.from_bytes(
            work_msg.coinb1 + enonce1 + enonce2 + work_msg.coinb2)
        cb = CompactBlock(work_msg.height,
                          work_msg.version,
                          Hash(work_msg.prev_block_hash),
                          work_msg.ntime,
                          work_msg.nbits,  # lower difficulty work for testing
                          work_msg.merkle_edge,
                          cb_txn)

        row_counter = 0
        for nonce in range(0xffffffff):

            if nonce % 6e3 == 0:
                logger.info(click.style(u'█', fg='green'), nl=False)
                row_counter += 1
            if row_counter > 40:
                row_counter = 0
                logger.info("")

            cb.block_header.nonce = nonce
            h = cb.block_header.hash.to_int('little')
            if h < pool_target:
                share = Share(
                    enonce2=enonce2,
                    nonce=nonce,
                    work_id=work_msg.work_id,
                    otime=int(time.time()))
                # adds a new line at the end of progress bar
                logger.info("")
                return share

        logger.info("Exhausted enonce1 space. Changing enonce2")
示例#5
0
    def from_bytes(b):
        """ Creates a Block from a serialized byte stream.

        Args:
            b (bytes): The byte stream, starting with the block version.

        Returns:
            block, b (tuple): A tuple. The first item is the deserialized block
            and the second is the remainder of the byte stream.
        """
        bh, b = BlockHeader.from_bytes(b)
        num_txns, b = unpack_compact_int(b)
        txns = []
        for i in range(num_txns):
            t, b = Transaction.from_bytes(b)
            txns.append(t)

        return Block.from_blockheader(bh, txns), b
示例#6
0
文件: block.py 项目: shayanb/two1
    def from_bytes(b):
        """ Creates a Block from a serialized byte stream.

        Args:
            b (bytes): The byte stream, starting with the block version.

        Returns:
            block, b (tuple): A tuple. The first item is the deserialized block
            and the second is the remainder of the byte stream.
        """
        bh, b = BlockHeader.from_bytes(b)
        num_txns, b = unpack_compact_int(b)
        txns = []
        for i in range(num_txns):
            t, b = Transaction.from_bytes(b)
            txns.append(t)

        return Block.from_blockheader(bh, txns), b