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 *
12 class ZMQTest (BitcoinTestFramework
):
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
)],
36 genhashes
= self
.nodes
[0].generate(1)
39 self
.log
.info("listen...")
40 msg
= self
.zmqSubSocket
.recv_multipart()
42 assert_equal(topic
, b
"hashtx")
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()
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
57 genhashes
= self
.nodes
[1].generate(n
)
62 for x
in range(0,n
*2):
63 msg
= self
.zmqSubSocket
.recv_multipart()
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)
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)
79 # now we should receive a zmq msg because the tx was broadcast
80 msg
= self
.zmqSubSocket
.recv_multipart()
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__':