10
10
from test_framework .test_framework import BitcoinTestFramework , SkipTest
11
11
from test_framework .util import (assert_equal ,
12
12
bytes_to_hex_str ,
13
- )
13
+ hash256 ,
14
+ )
14
15
15
16
class ZMQTest (BitcoinTestFramework ):
16
17
def set_test_params (self ):
@@ -37,9 +38,12 @@ def setup_nodes(self):
37
38
self .zmqSubSocket .set (zmq .RCVTIMEO , 60000 )
38
39
self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"hashblock" )
39
40
self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"hashtx" )
41
+ self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"rawblock" )
42
+ self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"rawtx" )
40
43
ip_address = "tcp://127.0.0.1:28332"
41
44
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 ], []]
43
47
self .add_nodes (self .num_nodes , self .extra_args )
44
48
self .start_nodes ()
45
49
@@ -59,28 +63,51 @@ def _zmq_test(self):
59
63
msg = self .zmqSubSocket .recv_multipart ()
60
64
topic = msg [0 ]
61
65
assert_equal (topic , b"hashtx" )
62
- body = msg [1 ]
66
+ txhash = msg [1 ]
63
67
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
64
68
assert_equal (msgSequence , 0 ) # must be sequence 0 on hashtx
65
69
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
+
66
81
self .log .info ("Wait for block" )
67
82
msg = self .zmqSubSocket .recv_multipart ()
68
83
topic = msg [0 ]
84
+ assert_equal (topic , b"hashblock" )
69
85
body = msg [1 ]
70
86
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
71
87
assert_equal (msgSequence , 0 ) # must be sequence 0 on hashblock
72
88
blkhash = bytes_to_hex_str (body )
73
-
74
89
assert_equal (genhashes [0 ], blkhash ) # blockhash from generate must be equal to the hash received over zmq
75
90
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
+
76
102
self .log .info ("Generate 10 blocks (and 10 coinbase txes)" )
77
103
n = 10
78
104
genhashes = self .nodes [1 ].generate (n )
79
105
self .sync_all ()
80
106
81
107
zmqHashes = []
108
+ zmqRawHashed = []
82
109
blockcount = 0
83
- for x in range (n * 2 ):
110
+ for x in range (n * 4 ):
84
111
msg = self .zmqSubSocket .recv_multipart ()
85
112
topic = msg [0 ]
86
113
body = msg [1 ]
@@ -89,9 +116,14 @@ def _zmq_test(self):
89
116
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
90
117
assert_equal (msgSequence , blockcount + 1 )
91
118
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 )
92
123
93
124
for x in range (n ):
94
125
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 ])
95
127
96
128
self .log .info ("Wait for tx from second node" )
97
129
# test tx from a second node
@@ -101,13 +133,21 @@ def _zmq_test(self):
101
133
# now we should receive a zmq msg because the tx was broadcast
102
134
msg = self .zmqSubSocket .recv_multipart ()
103
135
topic = msg [0 ]
104
- body = msg [1 ]
105
136
assert_equal (topic , b"hashtx" )
137
+ body = msg [1 ]
106
138
hashZMQ = bytes_to_hex_str (body )
107
139
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
108
140
assert_equal (msgSequence , blockcount + 1 )
109
141
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 )
110
149
assert_equal (hashRPC , hashZMQ ) # txid from sendtoaddress must be equal to the hash received over zmq
150
+ assert_equal (hashRPC , hashedZMQ )
111
151
112
152
if __name__ == '__main__' :
113
153
ZMQTest ().main ()
0 commit comments