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."""
10 from test_framework
.test_framework
import BitcoinTestFramework
, SkipTest
11 from test_framework
.util
import (assert_equal
,
15 class ZMQTest (BitcoinTestFramework
):
16 def set_test_params(self
):
19 def setup_nodes(self
):
20 # Try to import python3-zmq. Skip this test if the import fails.
24 raise SkipTest("python3-zmq module not available.")
26 # Check that bitcoin has been built with ZMQ enabled
27 config
= configparser
.ConfigParser()
28 if not self
.options
.configfile
:
29 self
.options
.configfile
= os
.path
.dirname(__file__
) + "/../config.ini"
30 config
.read_file(open(self
.options
.configfile
))
32 if not config
["components"].getboolean("ENABLE_ZMQ"):
33 raise SkipTest("bitcoind has not been built with zmq enabled.")
35 self
.zmqContext
= zmq
.Context()
36 self
.zmqSubSocket
= self
.zmqContext
.socket(zmq
.SUB
)
37 self
.zmqSubSocket
.set(zmq
.RCVTIMEO
, 60000)
38 self
.zmqSubSocket
.setsockopt(zmq
.SUBSCRIBE
, b
"hashblock")
39 self
.zmqSubSocket
.setsockopt(zmq
.SUBSCRIBE
, b
"hashtx")
40 ip_address
= "tcp://127.0.0.1:28332"
41 self
.zmqSubSocket
.connect(ip_address
)
42 self
.extra_args
= [['-zmqpubhashtx=%s' % ip_address
, '-zmqpubhashblock=%s' % ip_address
], []]
43 self
.add_nodes(self
.num_nodes
, self
.extra_args
)
50 # Destroy the zmq context
51 self
.log
.debug("Destroying zmq context")
52 self
.zmqContext
.destroy(linger
=None)
55 genhashes
= self
.nodes
[0].generate(1)
58 self
.log
.info("Wait for tx")
59 msg
= self
.zmqSubSocket
.recv_multipart()
61 assert_equal(topic
, b
"hashtx")
63 msgSequence
= struct
.unpack('<I', msg
[-1])[-1]
64 assert_equal(msgSequence
, 0) # must be sequence 0 on hashtx
66 self
.log
.info("Wait for block")
67 msg
= self
.zmqSubSocket
.recv_multipart()
70 msgSequence
= struct
.unpack('<I', msg
[-1])[-1]
71 assert_equal(msgSequence
, 0) # must be sequence 0 on hashblock
72 blkhash
= bytes_to_hex_str(body
)
74 assert_equal(genhashes
[0], blkhash
) # blockhash from generate must be equal to the hash received over zmq
76 self
.log
.info("Generate 10 blocks (and 10 coinbase txes)")
78 genhashes
= self
.nodes
[1].generate(n
)
83 for x
in range(n
* 2):
84 msg
= self
.zmqSubSocket
.recv_multipart()
87 if topic
== b
"hashblock":
88 zmqHashes
.append(bytes_to_hex_str(body
))
89 msgSequence
= struct
.unpack('<I', msg
[-1])[-1]
90 assert_equal(msgSequence
, blockcount
+ 1)
94 assert_equal(genhashes
[x
], zmqHashes
[x
]) # blockhash from generate must be equal to the hash received over zmq
96 self
.log
.info("Wait for tx from second node")
97 # test tx from a second node
98 hashRPC
= self
.nodes
[1].sendtoaddress(self
.nodes
[0].getnewaddress(), 1.0)
101 # now we should receive a zmq msg because the tx was broadcast
102 msg
= self
.zmqSubSocket
.recv_multipart()
105 assert_equal(topic
, b
"hashtx")
106 hashZMQ
= bytes_to_hex_str(body
)
107 msgSequence
= struct
.unpack('<I', msg
[-1])[-1]
108 assert_equal(msgSequence
, blockcount
+ 1)
110 assert_equal(hashRPC
, hashZMQ
) # txid from sendtoaddress must be equal to the hash received over zmq
112 if __name__
== '__main__':