Skip to content

Commit 5ebd5f9

Browse files
committed
[tests] tidy up zmq_test.py
1 parent 4a0c08f commit 5ebd5f9

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

test/functional/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
'rawtransactions.py',
7979
'reindex.py',
8080
# vv Tests less than 30s vv
81-
"zmq_test.py",
81+
'zmq_test.py',
8282
'mempool_resurrect_test.py',
8383
'txn_doublespend.py --mineblock',
8484
'txn_clone.py',

test/functional/zmq_test.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import struct
99

1010
from test_framework.test_framework import BitcoinTestFramework, SkipTest
11-
from test_framework.util import *
11+
from test_framework.util import (assert_equal,
12+
bytes_to_hex_str,
13+
)
1214

1315
class ZMQTest (BitcoinTestFramework):
1416

1517
def __init__(self):
1618
super().__init__()
17-
self.num_nodes = 4
18-
19-
port = 28332
19+
self.num_nodes = 2
2020

2121
def setup_nodes(self):
2222
# Try to import python3-zmq. Skip this test if the import fails.
@@ -38,57 +38,55 @@ def setup_nodes(self):
3838
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
3939
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
4040
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
41-
self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
42-
self.nodes = self.start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[
43-
['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
44-
[],
45-
[],
46-
[]
47-
])
41+
ip_address = "tcp://127.0.0.1:28332"
42+
self.zmqSubSocket.connect(ip_address)
43+
extra_args = [['-zmqpubhashtx=%s' % ip_address, '-zmqpubhashblock=%s' % ip_address], []]
44+
self.nodes = self.start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
4845

4946
def run_test(self):
50-
self.sync_all()
51-
5247
genhashes = self.nodes[0].generate(1)
5348
self.sync_all()
5449

55-
self.log.info("listen...")
50+
self.log.info("Wait for tx")
5651
msg = self.zmqSubSocket.recv_multipart()
5752
topic = msg[0]
5853
assert_equal(topic, b"hashtx")
5954
body = msg[1]
6055
msgSequence = struct.unpack('<I', msg[-1])[-1]
61-
assert_equal(msgSequence, 0) #must be sequence 0 on hashtx
56+
assert_equal(msgSequence, 0) # must be sequence 0 on hashtx
6257

58+
self.log.info("Wait for block")
6359
msg = self.zmqSubSocket.recv_multipart()
6460
topic = msg[0]
6561
body = msg[1]
6662
msgSequence = struct.unpack('<I', msg[-1])[-1]
67-
assert_equal(msgSequence, 0) #must be sequence 0 on hashblock
63+
assert_equal(msgSequence, 0) # must be sequence 0 on hashblock
6864
blkhash = bytes_to_hex_str(body)
6965

70-
assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
66+
assert_equal(genhashes[0], blkhash) # blockhash from generate must be equal to the hash received over zmq
7167

68+
self.log.info("Generate 10 blocks (and 10 coinbase txes)")
7269
n = 10
7370
genhashes = self.nodes[1].generate(n)
7471
self.sync_all()
7572

7673
zmqHashes = []
7774
blockcount = 0
78-
for x in range(0,n*2):
75+
for x in range(n * 2):
7976
msg = self.zmqSubSocket.recv_multipart()
8077
topic = msg[0]
8178
body = msg[1]
8279
if topic == b"hashblock":
8380
zmqHashes.append(bytes_to_hex_str(body))
8481
msgSequence = struct.unpack('<I', msg[-1])[-1]
85-
assert_equal(msgSequence, blockcount+1)
82+
assert_equal(msgSequence, blockcount + 1)
8683
blockcount += 1
8784

88-
for x in range(0,n):
89-
assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
85+
for x in range(n):
86+
assert_equal(genhashes[x], zmqHashes[x]) # blockhash from generate must be equal to the hash received over zmq
9087

91-
#test tx from a second node
88+
self.log.info("Wait for tx from second node")
89+
# test tx from a second node
9290
hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
9391
self.sync_all()
9492

@@ -100,10 +98,9 @@ def run_test(self):
10098
if topic == b"hashtx":
10199
hashZMQ = bytes_to_hex_str(body)
102100
msgSequence = struct.unpack('<I', msg[-1])[-1]
103-
assert_equal(msgSequence, blockcount+1)
104-
105-
assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
101+
assert_equal(msgSequence, blockcount + 1)
106102

103+
assert_equal(hashRPC, hashZMQ) # txid from sendtoaddress must be equal to the hash received over zmq
107104

108105
if __name__ == '__main__':
109-
ZMQTest ().main ()
106+
ZMQTest().main()

0 commit comments

Comments
 (0)