Skip to content

Commit d3677ab

Browse files
committed
Tests for zmqpubrawtx and zmqpubrawblock
1 parent bcc8a62 commit d3677ab

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

test/functional/test_framework/util.py

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from base64 import b64encode
88
from binascii import hexlify, unhexlify
99
from decimal import Decimal, ROUND_DOWN
10+
import hashlib
1011
import json
1112
import logging
1213
import os
@@ -148,6 +149,13 @@ def count_bytes(hex_string):
148149
def bytes_to_hex_str(byte_str):
149150
return hexlify(byte_str).decode('ascii')
150151

152+
def hash256(byte_str):
153+
sha256 = hashlib.sha256()
154+
sha256.update(byte_str)
155+
sha256d = hashlib.sha256()
156+
sha256d.update(sha256.digest())
157+
return sha256d.digest()[::-1]
158+
151159
def hex_str_to_bytes(hex_str):
152160
return unhexlify(hex_str.encode('ascii'))
153161

test/functional/zmq_test.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from test_framework.test_framework import BitcoinTestFramework, SkipTest
1111
from test_framework.util import (assert_equal,
1212
bytes_to_hex_str,
13-
)
13+
hash256,
14+
)
1415

1516
class 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

112152
if __name__ == '__main__':
113153
ZMQTest().main()

0 commit comments

Comments
 (0)