1010from test_framework .test_framework import BitcoinTestFramework , SkipTest
1111from test_framework .util import (assert_equal ,
1212 bytes_to_hex_str ,
13- )
13+ hash256 ,
14+ )
1415
1516class ZMQTest (BitcoinTestFramework ):
1617 def set_test_params (self ):
@@ -37,9 +38,12 @@ def setup_nodes(self):
3738 self .zmqSubSocket .set (zmq .RCVTIMEO , 60000 )
3839 self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"hashblock" )
3940 self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"hashtx" )
41+ self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"rawblock" )
42+ self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"rawtx" )
4043 ip_address = "tcp://127.0.0.1:28332"
4144 self .zmqSubSocket .connect (ip_address )
42- self .extra_args = [['-zmqpubhashtx=%s' % ip_address , '-zmqpubhashblock=%s' % ip_address ], []]
45+ self .extra_args = [['-zmqpubhashblock=%s' % ip_address , '-zmqpubhashtx=%s' % ip_address ,
46+ '-zmqpubrawblock=%s' % ip_address , '-zmqpubrawtx=%s' % ip_address ], []]
4347 self .add_nodes (self .num_nodes , self .extra_args )
4448 self .start_nodes ()
4549
@@ -59,28 +63,51 @@ def _zmq_test(self):
5963 msg = self .zmqSubSocket .recv_multipart ()
6064 topic = msg [0 ]
6165 assert_equal (topic , b"hashtx" )
62- body = msg [1 ]
66+ txhash = msg [1 ]
6367 msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
6468 assert_equal (msgSequence , 0 ) # must be sequence 0 on hashtx
6569
70+ # rawtx
71+ msg = self .zmqSubSocket .recv_multipart ()
72+ topic = msg [0 ]
73+ assert_equal (topic , b"rawtx" )
74+ body = msg [1 ]
75+ msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
76+ assert_equal (msgSequence , 0 ) # must be sequence 0 on rawtx
77+
78+ # Check that the rawtx hashes to the hashtx
79+ assert_equal (hash256 (body ), txhash )
80+
6681 self .log .info ("Wait for block" )
6782 msg = self .zmqSubSocket .recv_multipart ()
6883 topic = msg [0 ]
84+ assert_equal (topic , b"hashblock" )
6985 body = msg [1 ]
7086 msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
7187 assert_equal (msgSequence , 0 ) # must be sequence 0 on hashblock
7288 blkhash = bytes_to_hex_str (body )
73-
7489 assert_equal (genhashes [0 ], blkhash ) # blockhash from generate must be equal to the hash received over zmq
7590
91+ # rawblock
92+ msg = self .zmqSubSocket .recv_multipart ()
93+ topic = msg [0 ]
94+ assert_equal (topic , b"rawblock" )
95+ body = msg [1 ]
96+ msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
97+ assert_equal (msgSequence , 0 ) #must be sequence 0 on rawblock
98+
99+ # Check the hash of the rawblock's header matches generate
100+ assert_equal (genhashes [0 ], bytes_to_hex_str (hash256 (body [:80 ])))
101+
76102 self .log .info ("Generate 10 blocks (and 10 coinbase txes)" )
77103 n = 10
78104 genhashes = self .nodes [1 ].generate (n )
79105 self .sync_all ()
80106
81107 zmqHashes = []
108+ zmqRawHashed = []
82109 blockcount = 0
83- for x in range (n * 2 ):
110+ for x in range (n * 4 ):
84111 msg = self .zmqSubSocket .recv_multipart ()
85112 topic = msg [0 ]
86113 body = msg [1 ]
@@ -89,9 +116,14 @@ def _zmq_test(self):
89116 msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
90117 assert_equal (msgSequence , blockcount + 1 )
91118 blockcount += 1
119+ if topic == b"rawblock" :
120+ zmqRawHashed .append (bytes_to_hex_str (hash256 (body [:80 ])))
121+ msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
122+ assert_equal (msgSequence , blockcount )
92123
93124 for x in range (n ):
94125 assert_equal (genhashes [x ], zmqHashes [x ]) # blockhash from generate must be equal to the hash received over zmq
126+ assert_equal (genhashes [x ], zmqRawHashed [x ])
95127
96128 self .log .info ("Wait for tx from second node" )
97129 # test tx from a second node
@@ -101,13 +133,21 @@ def _zmq_test(self):
101133 # now we should receive a zmq msg because the tx was broadcast
102134 msg = self .zmqSubSocket .recv_multipart ()
103135 topic = msg [0 ]
104- body = msg [1 ]
105136 assert_equal (topic , b"hashtx" )
137+ body = msg [1 ]
106138 hashZMQ = bytes_to_hex_str (body )
107139 msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
108140 assert_equal (msgSequence , blockcount + 1 )
109141
142+ msg = self .zmqSubSocket .recv_multipart ()
143+ topic = msg [0 ]
144+ assert_equal (topic , b"rawtx" )
145+ body = msg [1 ]
146+ hashedZMQ = bytes_to_hex_str (hash256 (body ))
147+ msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
148+ assert_equal (msgSequence , blockcount + 1 )
110149 assert_equal (hashRPC , hashZMQ ) # txid from sendtoaddress must be equal to the hash received over zmq
150+ assert_equal (hashRPC , hashedZMQ )
111151
112152if __name__ == '__main__' :
113153 ZMQTest ().main ()
0 commit comments