|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2014-2016 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +import array |
| 7 | +import binascii |
| 8 | +import asyncio, zmq, zmq.asyncio |
| 9 | +import struct |
| 10 | + |
| 11 | +port = 28332 |
| 12 | + |
| 13 | +zmqContext = zmq.asyncio.Context() |
| 14 | + |
| 15 | +async def recv_and_process(): |
| 16 | + zmqSubSocket = zmqContext.socket(zmq.SUB) |
| 17 | + zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "hashblock") |
| 18 | + zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "hashtx") |
| 19 | + zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "rawblock") |
| 20 | + zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "rawtx") |
| 21 | + zmqSubSocket.connect("tcp://127.0.0.1:%i" % port) |
| 22 | + |
| 23 | + poller = zmq.asyncio.Poller() |
| 24 | + poller.register(zmqSubSocket, zmq.POLLIN) |
| 25 | + while True: |
| 26 | + s = await poller.poll() |
| 27 | + msg = await s[0][0].recv_multipart() |
| 28 | + topic = msg[0] |
| 29 | + body = msg[1] |
| 30 | + sequence = "Unknown"; |
| 31 | + if len(msg[-1]) == 4: |
| 32 | + msgSequence = struct.unpack('<I', msg[-1])[-1] |
| 33 | + sequence = str(msgSequence) |
| 34 | + if topic == b"hashblock": |
| 35 | + print('- HASH BLOCK ('+sequence+') -') |
| 36 | + print(binascii.hexlify(body)) |
| 37 | + elif topic == b"hashtx": |
| 38 | + print('- HASH TX ('+sequence+') -') |
| 39 | + print(binascii.hexlify(body)) |
| 40 | + elif topic == b"rawblock": |
| 41 | + print('- RAW BLOCK HEADER ('+sequence+') -') |
| 42 | + print(binascii.hexlify(body[:80])) |
| 43 | + elif topic == b"rawtx": |
| 44 | + print('- RAW TX ('+sequence+') -') |
| 45 | + print(binascii.hexlify(body)) |
| 46 | + |
| 47 | +try: |
| 48 | + loop = zmq.asyncio.ZMQEventLoop() |
| 49 | + asyncio.set_event_loop(loop) |
| 50 | + loop.run_until_complete(setup()) |
| 51 | +except KeyboardInterrupt: |
| 52 | + zmqContext.destroy() |
0 commit comments