[tests] Remove unused variables
[bitcoinplatinum.git] / test / functional / zmq_test.py
blob9e27b46381dc42419268ca90ed77e3301d4b5293
1 #!/usr/bin/env python3
2 # Copyright (c) 2015-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 """Test the ZMQ API."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
9 import zmq
10 import struct
12 class ZMQTest (BitcoinTestFramework):
14 def __init__(self):
15 super().__init__()
16 self.num_nodes = 4
18 port = 28332
20 def setup_nodes(self):
21 self.zmqContext = zmq.Context()
22 self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
23 self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
24 self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
25 self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
26 return start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[
27 ['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
28 [],
29 [],
33 def run_test(self):
34 self.sync_all()
36 genhashes = self.nodes[0].generate(1)
37 self.sync_all()
39 self.log.info("listen...")
40 msg = self.zmqSubSocket.recv_multipart()
41 topic = msg[0]
42 assert_equal(topic, b"hashtx")
43 body = msg[1]
44 msgSequence = struct.unpack('<I', msg[-1])[-1]
45 assert_equal(msgSequence, 0) #must be sequence 0 on hashtx
47 msg = self.zmqSubSocket.recv_multipart()
48 topic = msg[0]
49 body = msg[1]
50 msgSequence = struct.unpack('<I', msg[-1])[-1]
51 assert_equal(msgSequence, 0) #must be sequence 0 on hashblock
52 blkhash = bytes_to_hex_str(body)
54 assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
56 n = 10
57 genhashes = self.nodes[1].generate(n)
58 self.sync_all()
60 zmqHashes = []
61 blockcount = 0
62 for x in range(0,n*2):
63 msg = self.zmqSubSocket.recv_multipart()
64 topic = msg[0]
65 body = msg[1]
66 if topic == b"hashblock":
67 zmqHashes.append(bytes_to_hex_str(body))
68 msgSequence = struct.unpack('<I', msg[-1])[-1]
69 assert_equal(msgSequence, blockcount+1)
70 blockcount += 1
72 for x in range(0,n):
73 assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
75 #test tx from a second node
76 hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
77 self.sync_all()
79 # now we should receive a zmq msg because the tx was broadcast
80 msg = self.zmqSubSocket.recv_multipart()
81 topic = msg[0]
82 body = msg[1]
83 hashZMQ = ""
84 if topic == b"hashtx":
85 hashZMQ = bytes_to_hex_str(body)
86 msgSequence = struct.unpack('<I', msg[-1])[-1]
87 assert_equal(msgSequence, blockcount+1)
89 assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
92 if __name__ == '__main__':
93 ZMQTest ().main ()