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.
@@ -28,82 +28,87 @@ def setup_nodes(self):
28
28
# Check that bitcoin has been built with ZMQ enabled
29
29
config = configparser .ConfigParser ()
30
30
if not self .options .configfile :
31
- self .options .configfile = os .path .dirname (__file__ ) + "/config.ini"
31
+ self .options .configfile = os .path .dirname (__file__ ) + "/../ config.ini"
32
32
config .read_file (open (self .options .configfile ))
33
33
34
34
if not config ["components" ].getboolean ("ENABLE_ZMQ" ):
35
35
raise SkipTest ("bitcoind has not been built with zmq enabled." )
36
36
37
37
self .zmqContext = zmq .Context ()
38
38
self .zmqSubSocket = self .zmqContext .socket (zmq .SUB )
39
+ self .zmqSubSocket .set (zmq .RCVTIMEO , 60000 )
39
40
self .zmqSubSocket .setsockopt (zmq .SUBSCRIBE , b"hashblock" )
40
41
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
- ])
42
+ ip_address = "tcp://127.0.0.1:28332"
43
+ self .zmqSubSocket .connect (ip_address )
44
+ extra_args = [['-zmqpubhashtx=%s' % ip_address , '-zmqpubhashblock=%s' % ip_address ], []]
45
+ self .nodes = self .start_nodes (self .num_nodes , self .options .tmpdir , extra_args )
48
46
49
47
def run_test (self ):
50
- self .sync_all ()
48
+ try :
49
+ self ._zmq_test ()
50
+ finally :
51
+ # Destroy the zmq context
52
+ self .log .debug ("Destroying zmq context" )
53
+ self .zmqContext .destroy (linger = None )
51
54
55
+ def _zmq_test (self ):
52
56
genhashes = self .nodes [0 ].generate (1 )
53
57
self .sync_all ()
54
58
55
- self .log .info ("listen... " )
59
+ self .log .info ("Wait for tx " )
56
60
msg = self .zmqSubSocket .recv_multipart ()
57
61
topic = msg [0 ]
58
62
assert_equal (topic , b"hashtx" )
59
63
body = msg [1 ]
60
64
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
61
- assert_equal (msgSequence , 0 ) # must be sequence 0 on hashtx
65
+ assert_equal (msgSequence , 0 ) # must be sequence 0 on hashtx
62
66
67
+ self .log .info ("Wait for block" )
63
68
msg = self .zmqSubSocket .recv_multipart ()
64
69
topic = msg [0 ]
65
70
body = msg [1 ]
66
71
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
67
- assert_equal (msgSequence , 0 ) # must be sequence 0 on hashblock
72
+ assert_equal (msgSequence , 0 ) # must be sequence 0 on hashblock
68
73
blkhash = bytes_to_hex_str (body )
69
74
70
- assert_equal (genhashes [0 ], blkhash ) # blockhash from generate must be equal to the hash received over zmq
75
+ assert_equal (genhashes [0 ], blkhash ) # blockhash from generate must be equal to the hash received over zmq
71
76
77
+ self .log .info ("Generate 10 blocks (and 10 coinbase txes)" )
72
78
n = 10
73
79
genhashes = self .nodes [1 ].generate (n )
74
80
self .sync_all ()
75
81
76
82
zmqHashes = []
77
83
blockcount = 0
78
- for x in range (0 , n * 2 ):
84
+ for x in range (n * 2 ):
79
85
msg = self .zmqSubSocket .recv_multipart ()
80
86
topic = msg [0 ]
81
87
body = msg [1 ]
82
88
if topic == b"hashblock" :
83
89
zmqHashes .append (bytes_to_hex_str (body ))
84
90
msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
85
- assert_equal (msgSequence , blockcount + 1 )
91
+ assert_equal (msgSequence , blockcount + 1 )
86
92
blockcount += 1
87
93
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
94
+ for x in range (n ):
95
+ assert_equal (genhashes [x ], zmqHashes [x ]) # blockhash from generate must be equal to the hash received over zmq
90
96
91
- #test tx from a second node
97
+ self .log .info ("Wait for tx from second node" )
98
+ # test tx from a second node
92
99
hashRPC = self .nodes [1 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1.0 )
93
100
self .sync_all ()
94
101
95
102
# now we should receive a zmq msg because the tx was broadcast
96
103
msg = self .zmqSubSocket .recv_multipart ()
97
104
topic = msg [0 ]
98
105
body = msg [1 ]
99
- hashZMQ = ""
100
- if topic == b"hashtx" :
101
- hashZMQ = bytes_to_hex_str (body )
102
- 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
106
+ assert_equal (topic , b"hashtx" )
107
+ hashZMQ = bytes_to_hex_str (body )
108
+ msgSequence = struct .unpack ('<I' , msg [- 1 ])[- 1 ]
109
+ assert_equal (msgSequence , blockcount + 1 )
106
110
111
+ assert_equal (hashRPC , hashZMQ ) # txid from sendtoaddress must be equal to the hash received over zmq
107
112
108
113
if __name__ == '__main__' :
109
- ZMQTest ().main ()
114
+ ZMQTest ().main ()
0 commit comments