8
8
from test_framework .address import ADDRESS_BCRT1_UNSPENDABLE
9
9
from test_framework .test_framework import BitcoinTestFramework
10
10
from test_framework .messages import CTransaction , hash256
11
- from test_framework .util import assert_equal
11
+ from test_framework .util import assert_equal , connect_nodes
12
12
from io import BytesIO
13
13
14
- ADDRESS = "tcp://127.0.0.1:28332"
15
-
16
14
def hash256_reversed (byte_str ):
17
15
return hash256 (byte_str )[::- 1 ]
18
16
@@ -43,66 +41,61 @@ def skip_test_if_missing_module(self):
43
41
self .skip_if_no_py3_zmq ()
44
42
self .skip_if_no_bitcoind_zmq ()
45
43
46
- def setup_nodes (self ):
44
+ def run_test (self ):
47
45
import zmq
46
+ self .ctx = zmq .Context ()
47
+ try :
48
+ self .test_basic ()
49
+ finally :
50
+ # Destroy the ZMQ context.
51
+ self .log .debug ("Destroying ZMQ context" )
52
+ self .ctx .destroy (linger = None )
48
53
49
- # Initialize ZMQ context and socket.
54
+ def test_basic ( self ):
50
55
# All messages are received in the same socket which means
51
56
# that this test fails if the publishing order changes.
52
57
# Note that the publishing order is not defined in the documentation and
53
58
# is subject to change.
54
- self .zmq_context = zmq .Context ()
55
- socket = self .zmq_context .socket (zmq .SUB )
59
+ import zmq
60
+ address = 'tcp://127.0.0.1:28332'
61
+ socket = self .ctx .socket (zmq .SUB )
56
62
socket .set (zmq .RCVTIMEO , 60000 )
57
- socket .connect (ADDRESS )
63
+ socket .connect (address )
58
64
59
65
# Subscribe to all available topics.
60
- self .hashblock = ZMQSubscriber (socket , b"hashblock" )
61
- self .hashtx = ZMQSubscriber (socket , b"hashtx" )
62
- self .rawblock = ZMQSubscriber (socket , b"rawblock" )
63
- self .rawtx = ZMQSubscriber (socket , b"rawtx" )
64
-
65
- self .extra_args = [
66
- ["-zmqpub%s=%s" % (sub .topic .decode (), ADDRESS ) for sub in [self .hashblock , self .hashtx , self .rawblock , self .rawtx ]],
67
- [],
68
- ]
69
- self .add_nodes (self .num_nodes , self .extra_args )
70
- self .start_nodes ()
71
- self .import_deterministic_coinbase_privkeys ()
66
+ hashblock = ZMQSubscriber (socket , b"hashblock" )
67
+ hashtx = ZMQSubscriber (socket , b"hashtx" )
68
+ rawblock = ZMQSubscriber (socket , b"rawblock" )
69
+ rawtx = ZMQSubscriber (socket , b"rawtx" )
72
70
73
- def run_test (self ):
74
- try :
75
- self ._zmq_test ()
76
- finally :
77
- # Destroy the ZMQ context.
78
- self .log .debug ("Destroying ZMQ context" )
79
- self .zmq_context .destroy (linger = None )
71
+ self .restart_node (0 , ["-zmqpub%s=%s" % (sub .topic .decode (), address ) for sub in [hashblock , hashtx , rawblock , rawtx ]])
72
+ connect_nodes (self .nodes [0 ], 1 )
80
73
81
- def _zmq_test (self ):
82
74
num_blocks = 5
83
75
self .log .info ("Generate %(n)d blocks (and %(n)d coinbase txes)" % {"n" : num_blocks })
84
76
genhashes = self .nodes [0 ].generatetoaddress (num_blocks , ADDRESS_BCRT1_UNSPENDABLE )
77
+
85
78
self .sync_all ()
86
79
87
80
for x in range (num_blocks ):
88
81
# Should receive the coinbase txid.
89
- txid = self . hashtx .receive ()
82
+ txid = hashtx .receive ()
90
83
91
84
# Should receive the coinbase raw transaction.
92
- hex = self . rawtx .receive ()
85
+ hex = rawtx .receive ()
93
86
tx = CTransaction ()
94
87
tx .deserialize (BytesIO (hex ))
95
88
tx .calc_sha256 ()
96
89
assert_equal (tx .hash , txid .hex ())
97
90
98
91
# Should receive the generated block hash.
99
- hash = self . hashblock .receive ().hex ()
92
+ hash = hashblock .receive ().hex ()
100
93
assert_equal (genhashes [x ], hash )
101
94
# The block should only have the coinbase txid.
102
95
assert_equal ([txid .hex ()], self .nodes [1 ].getblock (hash )["tx" ])
103
96
104
97
# Should receive the generated raw block.
105
- block = self . rawblock .receive ()
98
+ block = rawblock .receive ()
106
99
assert_equal (genhashes [x ], hash256_reversed (block [:80 ]).hex ())
107
100
108
101
if self .is_wallet_compiled ():
@@ -111,20 +104,20 @@ def _zmq_test(self):
111
104
self .sync_all ()
112
105
113
106
# Should receive the broadcasted txid.
114
- txid = self . hashtx .receive ()
107
+ txid = hashtx .receive ()
115
108
assert_equal (payment_txid , txid .hex ())
116
109
117
110
# Should receive the broadcasted raw transaction.
118
- hex = self . rawtx .receive ()
111
+ hex = rawtx .receive ()
119
112
assert_equal (payment_txid , hash256_reversed (hex ).hex ())
120
113
121
114
122
115
self .log .info ("Test the getzmqnotifications RPC" )
123
116
assert_equal (self .nodes [0 ].getzmqnotifications (), [
124
- {"type" : "pubhashblock" , "address" : ADDRESS , "hwm" : 1000 },
125
- {"type" : "pubhashtx" , "address" : ADDRESS , "hwm" : 1000 },
126
- {"type" : "pubrawblock" , "address" : ADDRESS , "hwm" : 1000 },
127
- {"type" : "pubrawtx" , "address" : ADDRESS , "hwm" : 1000 },
117
+ {"type" : "pubhashblock" , "address" : address , "hwm" : 1000 },
118
+ {"type" : "pubhashtx" , "address" : address , "hwm" : 1000 },
119
+ {"type" : "pubrawblock" , "address" : address , "hwm" : 1000 },
120
+ {"type" : "pubrawtx" , "address" : address , "hwm" : 1000 },
128
121
])
129
122
130
123
assert_equal (self .nodes [1 ].getzmqnotifications (), [])
0 commit comments