Skip to content

Commit a0f4f9c

Browse files
committed
Add zmq test for transaction pub during reorg
1 parent 2399a06 commit a0f4f9c

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

test/functional/interface_zmq.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,55 @@ def test_basic(self):
144144
def test_reorg(self):
145145
import zmq
146146
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]
150160

151161
# 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)
154165
# Relax so that the subscriber is ready before publishing zmq messages
155166
sleep(0.2)
156167

157168
# 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]
159171
assert_equal(self.nodes[0].getbestblockhash(), hashblock.receive().hex())
172+
hashtx.receive() # consume, already tested
160173

161174
# 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)
163176

164177
# nodes[0] will reorg chain after connecting back nodes[1]
165178
connect_nodes(self.nodes[0], 1)
179+
self.sync_all()
166180

167181
# Should receive nodes[1] tip
168182
assert_equal(self.nodes[1].getbestblockhash(), hashblock.receive().hex())
169183

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+
170197
if __name__ == '__main__':
171198
ZMQTest().main()

0 commit comments

Comments
 (0)