Changeset View
Changeset View
Standalone View
Standalone View
test/functional/interface_zmq.py
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | class ZMQTest (BitcoinTestFramework): | ||||
def run_test(self): | def run_test(self): | ||||
self.ctx = zmq.Context() | self.ctx = zmq.Context() | ||||
try: | try: | ||||
self.test_basic() | self.test_basic() | ||||
self.test_sequence() | self.test_sequence() | ||||
self.test_mempool_sync() | self.test_mempool_sync() | ||||
self.test_reorg() | self.test_reorg() | ||||
self.test_multiple_interfaces() | |||||
finally: | finally: | ||||
# Destroy the ZMQ context. | # Destroy the ZMQ context. | ||||
self.log.debug("Destroying ZMQ context") | self.log.debug("Destroying ZMQ context") | ||||
self.ctx.destroy(linger=None) | self.ctx.destroy(linger=None) | ||||
def test_basic(self): | def test_basic(self): | ||||
# Invalid zmq arguments don't take down the node, see #17185. | # Invalid zmq arguments don't take down the node, see #17185. | ||||
▲ Show 20 Lines • Show All 516 Lines • ▼ Show 20 Lines | def test_mempool_sync(self): | ||||
self.nodes[0].getrawmempool( | self.nodes[0].getrawmempool( | ||||
mempool_sequence=True)["mempool_sequence"], | mempool_sequence=True)["mempool_sequence"], | ||||
expected_sequence) | expected_sequence) | ||||
# 5) If you miss a zmq/mempool sequence number, go back to step (2) | # 5) If you miss a zmq/mempool sequence number, go back to step (2) | ||||
self.nodes[0].generatetoaddress(1, ADDRESS_ECREG_UNSPENDABLE) | self.nodes[0].generatetoaddress(1, ADDRESS_ECREG_UNSPENDABLE) | ||||
def test_multiple_interfaces(self): | |||||
# Set up two subscribers with different addresses | |||||
subscribers = [] | |||||
for i in range(2): | |||||
address = f"tcp://127.0.0.1:{28334 + i}" | |||||
socket = self.ctx.socket(zmq.SUB) | |||||
socket.set(zmq.RCVTIMEO, 60000) | |||||
hashblock = ZMQSubscriber(socket, b"hashblock") | |||||
socket.connect(address) | |||||
subscribers.append({'address': address, 'hashblock': hashblock}) | |||||
self.restart_node( | |||||
0, | |||||
[f'-zmqpub{subscriber["hashblock"].topic.decode()}={subscriber["address"]}' | |||||
for subscriber in subscribers]) | |||||
# Relax so that the subscriber is ready before publishing zmq messages | |||||
sleep(0.2) | |||||
# Generate 1 block in nodes[0] and receive all notifications | |||||
self.nodes[0].generatetoaddress(1, ADDRESS_ECREG_UNSPENDABLE) | |||||
# Should receive the same block hash on both subscribers | |||||
assert_equal(self.nodes[0].getbestblockhash(), | |||||
subscribers[0]['hashblock'].receive().hex()) | |||||
assert_equal(self.nodes[0].getbestblockhash(), | |||||
subscribers[1]['hashblock'].receive().hex()) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
ZMQTest().main() | ZMQTest().main() |