|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2015 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 | +# |
| 7 | +# Test ZMQ interface |
| 8 | +# |
| 9 | + |
| 10 | +from test_framework import BitcoinTestFramework |
| 11 | +from util import * |
| 12 | +import json |
| 13 | +import zmq |
| 14 | +import binascii |
| 15 | +from mininode import hash256 |
| 16 | + |
| 17 | +try: |
| 18 | + import http.client as httplib |
| 19 | +except ImportError: |
| 20 | + import httplib |
| 21 | +try: |
| 22 | + import urllib.parse as urlparse |
| 23 | +except ImportError: |
| 24 | + import urlparse |
| 25 | + |
| 26 | +class ZMQTest (BitcoinTestFramework): |
| 27 | + |
| 28 | + port = 28332 |
| 29 | + |
| 30 | + def handleBLK(self, blk): |
| 31 | + return binascii.hexlify(hash256(blk[:80])[::-1]) |
| 32 | + |
| 33 | + |
| 34 | + def handleTX(self, tx): |
| 35 | + return binascii.hexlify(hash256(tx)[::-1]) |
| 36 | + |
| 37 | + def setup_nodes(self): |
| 38 | + self.zmqContext = zmq.Context() |
| 39 | + self.zmqSubSocket = self.zmqContext.socket(zmq.SUB) |
| 40 | + self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "BLK") |
| 41 | + self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "TXN") |
| 42 | + self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port) |
| 43 | + |
| 44 | + # Note: proxies are not used to connect to local nodes |
| 45 | + # this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost |
| 46 | + return start_nodes(4, self.options.tmpdir, extra_args=[ |
| 47 | + ['-zmqpub=tcp://127.0.0.1:'+str(self.port)], |
| 48 | + [], |
| 49 | + [], |
| 50 | + [] |
| 51 | + ]) |
| 52 | + |
| 53 | + def run_test(self): |
| 54 | + |
| 55 | + genhashes = self.nodes[0].generate(1); |
| 56 | + |
| 57 | + msg = self.zmqSubSocket.recv() |
| 58 | + blkhash = self.handleBLK(msg[3:]) |
| 59 | + assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq |
| 60 | + |
| 61 | + genhashes = self.nodes[1].generate(10); |
| 62 | + self.sync_all() |
| 63 | + |
| 64 | + zmqHashes = [] |
| 65 | + for x in range(0,10): |
| 66 | + msg = self.zmqSubSocket.recv() |
| 67 | + zmqHashes.append(self.handleBLK(msg[3:])) |
| 68 | + |
| 69 | + #sort hashes |
| 70 | + genhashes.sort() |
| 71 | + zmqHashes.sort() |
| 72 | + for x in range(0,9): |
| 73 | + assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq |
| 74 | + |
| 75 | + #test tx from a second node |
| 76 | + hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0) |
| 77 | + self.sync_all() |
| 78 | + |
| 79 | + #now we should receive a zmq msg because the tx was broadcastet |
| 80 | + msg = self.zmqSubSocket.recv() |
| 81 | + hashZMQ = self.handleTX(msg[3:]) |
| 82 | + assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + ZMQTest ().main () |
0 commit comments