Exemple #1
0
 def serialize(self, version_msg=False):
     msg = b""
     # FIXME: What's the right condition here
     if self.time:
         msg += int_to_little_endian(self.time, 4)
     msg += int_to_little_endian(self.services, 8)
     msg += int_to_little_endian(self.ip, 16)
     msg += int_to_little_endian(self.port, 2)
     return msg
Exemple #2
0
 def serialize(self):
     # version - 4 bytes, little endian
     result = int_to_little_endian(self.version, 4)
     # prev_block - 32 bytes, little endian
     result += int_to_little_endian(self.prev_block, 32)
     # merkle_root - 32 bytes, little endian
     result += int_to_little_endian(self.merkle_root, 32)
     # timestamp - 4 bytes, little endian
     result += int_to_little_endian(self.timestamp, 4)
     # bits - 4 bytes
     result += self.bits
     # nonce - 4 bytes
     result += self.nonce
     return result
Exemple #3
0
 def serialize(self):
     result = NETWORK_MAGIC
     result += encode_command(self.command)
     result += int_to_little_endian(len(self.payload), 4)
     result += double_sha256(self.payload)[:4]
     result += self.payload
     return result
Exemple #4
0
 def serialize(self):
     msg = b""
     msg += int_to_little_endian(self.version, 4)
     msg += int_to_little_endian(self.services, 8)
     msg += int_to_little_endian(self.timestamp, 8)
     msg += self.addr_recv.serialize()
     msg += self.addr_from.serialize()
     msg += int_to_little_endian(self.nonce, 8)
     msg += encode_varstr(self.user_agent)
     msg += int_to_little_endian(self.start_height, 4)
     msg += int_to_little_endian(self.relay, 1)
     return msg
def handle_headers(payload, sock):
    block_headers = Headers.parse(payload)
    print(f'{len(block_headers.headers)} new headers')
    update_blocks(block_headers)

    # after 500 headers, get the blocks
    if len(blocks) < 500:
        send_getheaders(sock)
    else:
        items = [
            InventoryItem(2, int_to_little_endian(hash_, 32))
            for hash_ in blocks[:10]
        ]
        getdata = GetData(items=items)
        msg = Message(getdata.command, getdata.serialize())
        sock.send(msg.serialize())

    print(f'We now have {len(blocks)} headers')
Exemple #6
0
 def serialize(self):
     msg = int_to_little_endian(self.version, 4)
     msg += encode_varint(len(self.items))
     for hash_ in self.items:
         msg += int_to_little_endian(hash_, 32)
     return msg
Exemple #7
0
 def serialize(self):
     msg = self.locator.serialize()
     msg += int_to_little_endian(self.hashstop, 32)
     return msg
Exemple #8
0
 def serialize(self):
     msg = b""
     msg += int_to_little_endian(self.type, 4)
     msg += self.hash
     return msg