Exemplo n.º 1
0
def main():
    miner = create_miner_based_on_cmd_parameters()

    pow_msg_id = b'1234'
    pow_message_history_id = b''
    pow_end_it_model_hash = double_sha256(b'5678')
    pow_local_message_map = [1, 2, 3, 4]

    try:
        for it in range(0, miner._iterations_announced):
            miner.announce_new_block()
            hexdata, nonce = miner.mine_announced_block(
                pow_msg_id, pow_message_history_id, pow_end_it_model_hash,
                pow_local_message_map)
            assert hexdata is False
            assert nonce is None

        for it in range(0, 1000):
            miner.announce_new_block()
            hexdata, nonce = miner.mine_announced_block(
                pow_msg_id, pow_message_history_id, pow_end_it_model_hash,
                pow_local_message_map)
            if hexdata:
                miner.submit_block(hexdata)
                print(
                    f"submit_block: nonce = {hexlify(nonce).decode('latin1')}")

    except JSONRPCException as e:
        print(e.error['message'])
    except Exception as e:
        print(e)
    return
Exemplo n.º 2
0
    def mine_announced_block(self, msg_id, message_history_id,
                             end_it_model_hash, local_message_map):
        if len(self._blocks) <= self._iterations_announced:
            return False, None

        while len(self._blocks) > self._iterations_announced + 1:
            self._blocks.pop(0)

        block = self._blocks.pop(0)

        nonce = self.calculate_nonce(end_it_model_hash, local_message_map)
        pouw = (len(message_history_id)).to_bytes(1, 'little') + message_history_id + \
               (len(msg_id)).to_bytes(1, 'little') + msg_id

        # add nonce and pouw fields
        header = block.header + nonce + pouw

        # test block hash
        blkhash = double_sha256(header)
        if not self._check_nonce(blkhash, block.target):
            return False, nonce

        hexdata = hexlify(header).decode('ascii')
        hexdata += block.hexdata
        return hexdata, nonce
Exemplo n.º 3
0
    def announce_new_block(self):
        if (self._template is None) or (self._template.version is None) or (
                time.time() - self._template.curtime >
                self._template_refresh_interval):
            self._get_block_template()

        block = Block(self._template)
        self._blocks.append(block)
        return hexlify(double_sha256(block.header)).decode('ascii')
Exemplo n.º 4
0
 def calculate_zero_nonce_hash(block_header_hex):
     block_header = unhexlify(block_header_hex)[:76]
     return hexlify(double_sha256(block_header)).decode('ascii')
Exemplo n.º 5
0
    template = Template()
    req = template.request()

    # send req to server and parse response into req
    send_json(req)
    if (len(sys.argv) == 2):
        req = json.load(sys.stdin)
    else:
        req = json.loads(test_input)
        send_json(req)

    template.add(req)
    while (template.time_left() and template.work_left()):
        (data, dataid) = template.get_data()
        assert (len(data) >= 76)

        # mine the right nonce
        for nonce in range(0x7fffffff):
            data = data[:76] + struct.pack('!I', nonce)
            blkhash = double_sha256(data)
            if blkhash[28:] == b'\0\0\0\0':
                break
            if (not (nonce % 0x1000)):
                sys.stdout.write("0x%8x hashes done...\r" % nonce)
                sys.stdout.flush()
        print("Found nonce: 0x%8x \n" % nonce)

        req = template.submit(data, dataid, nonce)
        # send req to server
        send_json(req)