def test_serialize(self): msg = unhexlify('f9beb4d976657261636b000000000000000000005df6e0e2') stream = BytesIO(msg) envelope = NetworkEnvelope.parse(stream) self.assertEqual(envelope.serialize(), msg) msg = unhexlify('f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001') stream = BytesIO(msg) envelope = NetworkEnvelope.parse(stream) self.assertEqual(envelope.serialize(), msg)
def get_blocks(sock): index = len(data["blocks"]) items = [ InventoryItem(2, int_to_little_endian(hash_, 32)) for hash_ in data["headers"][index:index + 10] ] getdata = GetData(items=items) packet = NetworkEnvelope(getdata.command, getdata.serialize()) sock.send(packet.serialize())
def test_parse(self): msg = unhexlify('f9beb4d976657261636b000000000000000000005df6e0e2') stream = BytesIO(msg) envelope = NetworkEnvelope.parse(stream) self.assertEqual(envelope.command[:6], b'verack') self.assertEqual(envelope.payload, b'') msg = unhexlify('f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001') stream = BytesIO(msg) envelope = NetworkEnvelope.parse(stream) self.assertEqual(envelope.command[:7], b'version') self.assertEqual(envelope.payload, msg[24:])
def handshake(address): # Create the socket sock = socket.socket() stream = sock.makefile('rb', None) # Initiate TCP connection sock.connect(address) # Send our "version" message payload = VersionMessage().serialize() envelope = NetworkEnvelope(command=b"version", payload=payload) sock.send(envelope.serialize()) # Receive peer's "version" message envelope = NetworkEnvelope.parse(stream) print(envelope.command) # Receive peer's "verack" message envelope = NetworkEnvelope.parse(stream) print(envelope.command) # Send our "verack" message envelope = NetworkEnvelope(command=b"verack", payload=b"") sock.send(envelope.serialize()) return sock, stream
def main(): address = ("91.221.70.137", 8333) sock, stream = handshake(address) send_getheaders(sock) while True: try: packet = NetworkEnvelope.parse(stream) except EOFError: print("Peer hung up") return except Exception as e: print(f'encountered "{e}" reading packet') handle_packet(packet, sock)
async def receive(self): print("start receiving") while True: magic = await self.reader.read(4) if magic != NETWORK_MAGIC: raise RuntimeError('Network Magic not at beginning of stream') command = await self.reader.read(12) payload_length = little_endian_to_int(await self.reader.read(4)) checksum = await self.reader.read(4) payload = await self.reader.read(payload_length) # check the checksum if double_sha256(payload)[:4] != checksum: raise RuntimeError('Payload and Checksum do not match') await self.q.put(NetworkEnvelope(command, payload))
def _connect(self): print(f"connecting to {self.address}") self.start = time.time() self.start_handshake() while not self.complete(): self.check_for_timeout() try: packet = NetworkEnvelope.parse(self.stream) except EOFError as e: # For now we ditch this connection raise e except Exception as e: continue self.handle_packet(packet)
import socket from network import NetworkEnvelope, VersionMessage host = 'testnet.programmingbitcoin.com' port = 18333 socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.connect((host, port)) stream = socket.makefile('rb', None) version = VersionMessage() envelope = NetworkEnvelope(version.command, version.serialize()) socket.sendall(envelope.serialize()) while True: new_message = NetworkEnvelope.parse(stream) print(new_message)
def send_getheaders(sock): locator = construct_block_locator() getheaders = GetHeaders(locator) msg = NetworkEnvelope(getheaders.command, getheaders.serialize()) sock.send(msg.serialize()) print("sent getheaders")
def test_exercise_1(self): message_hex = 'f9beb4d976657261636b000000000000000000005df6e0e2' stream = BytesIO(bytes.fromhex(message_hex)) envelope = NetworkEnvelope.parse(stream) self.assertEqual(envelope.command, b'verack') self.assertEqual(envelope.payload, b'')
def handle_verack(self, packet): my_getaddr_packet = NetworkEnvelope(command=b"getaddr", payload=b"") self.socket.send(my_getaddr_packet.serialize())
def handle_version(self, packet): self.version_message = packet.payload # FIXME: payload should default to b"" my_verack_packet = NetworkEnvelope(command=b"verack", payload=b"") self.socket.send(my_verack_packet.serialize())
from network import NetworkEnvelope from io import BytesIO message_hex = 'f9beb4d976657261636b000000000000000000005df6e0e2' envelope = NetworkEnvelope.parse(BytesIO(bytes.fromhex(message_hex))) print(envelope.command) print(envelope.payload)