@@ -144,28 +144,55 @@ def test_basic(self):
144
144
def test_reorg (self ):
145
145
import zmq
146
146
address = 'tcp://127.0.0.1:28333'
147
- socket = self .ctx .socket (zmq .SUB )
148
- socket .set (zmq .RCVTIMEO , 60000 )
149
- hashblock = ZMQSubscriber (socket , b'hashblock' )
147
+
148
+ services = [b"hashblock" , b"hashtx" ]
149
+ sockets = []
150
+ subs = []
151
+ for service in services :
152
+ sockets .append (self .ctx .socket (zmq .SUB ))
153
+ # 2 second timeout to check end of notifications
154
+ sockets [- 1 ].set (zmq .RCVTIMEO , 2000 )
155
+ subs .append (ZMQSubscriber (sockets [- 1 ], service ))
156
+
157
+ # Subscribe to all available topics.
158
+ hashblock = subs [0 ]
159
+ hashtx = subs [1 ]
150
160
151
161
# Should only notify the tip if a reorg occurs
152
- self .restart_node (0 , ['-zmqpub%s=%s' % (hashblock .topic .decode (), address )])
153
- socket .connect (address )
162
+ self .restart_node (0 , ["-zmqpub%s=%s" % (sub .topic .decode (), address ) for sub in [hashblock , hashtx ]])
163
+ for socket in sockets :
164
+ socket .connect (address )
154
165
# Relax so that the subscriber is ready before publishing zmq messages
155
166
sleep (0.2 )
156
167
157
168
# Generate 1 block in nodes[0] and receive all notifications
158
- self .nodes [0 ].generatetoaddress (1 , ADDRESS_BCRT1_UNSPENDABLE )
169
+ disconnect_block = self .nodes [0 ].generatetoaddress (1 , ADDRESS_BCRT1_UNSPENDABLE )[0 ]
170
+ disconnect_cb = self .nodes [0 ].getblock (disconnect_block )["tx" ][0 ]
159
171
assert_equal (self .nodes [0 ].getbestblockhash (), hashblock .receive ().hex ())
172
+ hashtx .receive () # consume, already tested
160
173
161
174
# Generate 2 blocks in nodes[1]
162
- self .nodes [1 ].generatetoaddress (2 , ADDRESS_BCRT1_UNSPENDABLE )
175
+ connect_blocks = self .nodes [1 ].generatetoaddress (2 , ADDRESS_BCRT1_UNSPENDABLE )
163
176
164
177
# nodes[0] will reorg chain after connecting back nodes[1]
165
178
connect_nodes (self .nodes [0 ], 1 )
179
+ self .sync_all ()
166
180
167
181
# Should receive nodes[1] tip
168
182
assert_equal (self .nodes [1 ].getbestblockhash (), hashblock .receive ().hex ())
169
183
184
+ # During reorg, should only receive the last coinbase tx from tip
185
+ assert_equal (hashtx .receive ().hex (), self .nodes [1 ].getblock (connect_blocks [1 ])["tx" ][0 ])
186
+
187
+ # But if we do a simple invalidate we announce the disconnected coinbase
188
+ self .nodes [0 ].invalidateblock (connect_blocks [1 ])
189
+ assert_equal (hashtx .receive ().hex (), self .nodes [1 ].getblock (connect_blocks [1 ])["tx" ][0 ])
190
+ # And not the current tip
191
+ try :
192
+ hashtx .receive ()
193
+ raise Exception ("Should have failed" )
194
+ except zmq .error .Again :
195
+ pass
196
+
170
197
if __name__ == '__main__' :
171
198
ZMQTest ().main ()
0 commit comments