8
8
import struct
9
9
10
10
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
+ )
12
14
13
15
class ZMQTest (BitcoinTestFramework ):
14
16
15
17
def __init__ (self ):
16
18
super ().__init__ ()
17
- self .num_nodes = 4
18
-
19
- port = 28332
19
+ self .num_nodes = 2
20
20
21
21
def setup_nodes (self ):
22
22
# Try to import python3-zmq. Skip this test if the import fails.
@@ -38,57 +38,55 @@ def setup_nodes(self):
38
38
self .zmqSubSocket = self .zmqContext .socket (zmq .SUB )
39
39
self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"hashblock" )
40
40
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 )
48
45
49
46
def run_test (self ):
50
- self .sync_all ()
51
-
52
47
genhashes = self .nodes [0 ].generate (1 )
53
48
self .sync_all ()
54
49
55
- self .log .info ("listen... " )
50
+ self .log .info ("Wait for tx " )
56
51
msg = self .zmqSubSocket .recv_multipart ()
57
52
topic = msg [0 ]
58
53
assert_equal (topic , b"hashtx" )
59
54
body = msg [1 ]
60
55
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
62
57
58
+ self .log .info ("Wait for block" )
63
59
msg = self .zmqSubSocket .recv_multipart ()
64
60
topic = msg [0 ]
65
61
body = msg [1 ]
66
62
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
68
64
blkhash = bytes_to_hex_str (body )
69
65
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
71
67
68
+ self .log .info ("Generate 10 blocks (and 10 coinbase txes)" )
72
69
n = 10
73
70
genhashes = self .nodes [1 ].generate (n )
74
71
self .sync_all ()
75
72
76
73
zmqHashes = []
77
74
blockcount = 0
78
- for x in range (0 , n * 2 ):
75
+ for x in range (n * 2 ):
79
76
msg = self .zmqSubSocket .recv_multipart ()
80
77
topic = msg [0 ]
81
78
body = msg [1 ]
82
79
if topic == b"hashblock" :
83
80
zmqHashes .append (bytes_to_hex_str (body ))
84
81
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
85
- assert_equal (msgSequence , blockcount + 1 )
82
+ assert_equal (msgSequence , blockcount + 1 )
86
83
blockcount += 1
87
84
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
90
87
91
- #test tx from a second node
88
+ self .log .info ("Wait for tx from second node" )
89
+ # test tx from a second node
92
90
hashRPC = self .nodes [1 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1.0 )
93
91
self .sync_all ()
94
92
@@ -100,10 +98,9 @@ def run_test(self):
100
98
if topic == b"hashtx" :
101
99
hashZMQ = bytes_to_hex_str (body )
102
100
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 )
106
102
103
+ assert_equal (hashRPC , hashZMQ ) # txid from sendtoaddress must be equal to the hash received over zmq
107
104
108
105
if __name__ == '__main__' :
109
- ZMQTest ().main ()
106
+ ZMQTest ().main ()
0 commit comments