From 6b5a0f67d779c4897095f3003d1f25d65b7edeea Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sat, 14 Nov 2026 03:06:04 +0100 Subject: [PATCH] tests: add zmq listening --- CMakeLists.txt | 28 ++++++++++++++++ external/monero-cpp | 2 +- src/cpp/common/py_monero_common_bindings.cpp | 24 ++++++++++++++ src/cpp/py_monero_types.h | 4 +++ src/python/__init__.pyi | 4 +++ src/python/monero_rpc_connection.pyi | 11 +++++++ src/python/monero_zmq_publication.pyi | 11 +++++++ src/python/monero_zmq_subscriber.pyi | 34 ++++++++++++++++++++ tests/docker-compose.yml | 6 ---- tests/test_monero_rpc_connection.py | 21 +++++++++++- tests/utils/test_utils.py | 9 ++++-- 11 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 src/python/monero_zmq_publication.pyi create mode 100644 src/python/monero_zmq_subscriber.pyi diff --git a/CMakeLists.txt b/CMakeLists.txt index 6227581..807a5f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -234,6 +234,11 @@ if (Boost_VERSION_STRING VERSION_LESS 1.69.0) list(APPEND BOOST_COMPONENTS system) endif() +# Boost Regex is header-only since 1.77 +if (Boost_VERSION_STRING VERSION_LESS 1.77.0) + list(APPEND BOOST_COMPONENTS regex) +endif() + find_package(Boost ${BOOST_MIN_VER} QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS}) message(STATUS "Boost components: ${BOOST_COMPONENTS}") message(STATUS "Using Boost include dir at ${Boost_INCLUDE_DIR}") @@ -292,6 +297,28 @@ message(STATUS "Using HIDAPI include dir at ${HIDAPI_INCLUDE_DIR}") find_library(SODIUM_LIBRARY sodium REQUIRED) message(STATUS "Using libsodium library at ${SODIUM_LIBRARY}") +############ +# libzmq +############ + +# link before libsodium, which static libzmq depends on +find_library(ZMQ_LIBRARY zmq REQUIRED) +set(ZMQ_LIBRARIES ${ZMQ_LIBRARY}) +if (ZMQ_LIBRARY MATCHES "\\.a$") + # optional transports of static libzmq + foreach(zmq_dep pgm norm gssapi_krb5 protolib protokit bsd md) + string(TOUPPER ${zmq_dep} zmq_dep_var) + find_library(ZMQ_DEP_${zmq_dep_var} ${zmq_dep}) + if (ZMQ_DEP_${zmq_dep_var}) + list(APPEND ZMQ_LIBRARIES ${ZMQ_DEP_${zmq_dep_var}}) + endif() + endforeach() +endif() +if (WIN32) + list(APPEND ZMQ_LIBRARIES ws2_32 rpcrt4 iphlpapi) +endif() +message(STATUS "Using libzmq libraries: ${ZMQ_LIBRARIES}") + ############ # Protobuf ############ @@ -375,6 +402,7 @@ set(MONERO_PYTHON_LINK_LIBS ${OPENSSL_LIBRARIES} ${Protobuf_LIBRARY} ${LibUSB_LIBRARIES} + ${ZMQ_LIBRARIES} ${SODIUM_LIBRARY} ${HIDAPI_LIBRARIES} ${UNBOUND_LIBRARIES} diff --git a/external/monero-cpp b/external/monero-cpp index 6178753..b6074b0 160000 --- a/external/monero-cpp +++ b/external/monero-cpp @@ -1 +1 @@ -Subproject commit 6178753bc9b1956b4d2e834507b6ff4cb55229c7 +Subproject commit b6074b0a3068b0f3e9ee0019ffd8e2d1050be15b diff --git a/src/cpp/common/py_monero_common_bindings.cpp b/src/cpp/common/py_monero_common_bindings.cpp index 334e7eb..d5291a5 100644 --- a/src/cpp/common/py_monero_common_bindings.cpp +++ b/src/cpp/common/py_monero_common_bindings.cpp @@ -252,6 +252,27 @@ void py_monero_bind_common(py::module_& m, PyMoneroTypes& t) { .def_readwrite("ssl_allowed_fingerprints", &ssl_options::m_ssl_allowed_fingerprints) .def_readwrite("ssl_allow_any_cert", &ssl_options::m_ssl_allow_any_cert); + // monero_zmq_publication + t.py_monero_zmq_publication + .def(py::init<>()) + .def_readwrite("topic", &monero_zmq_publication::m_topic) + .def_readwrite("json", &monero_zmq_publication::m_json); + + // monero_zmq_subscriber + t.py_monero_zmq_subscriber + .def("is_connected", [](const monero_zmq_subscriber& self) { + MONERO_CATCH_AND_RETHROW(self.is_connected()); + }) + .def("get_num_connections", [](const monero_zmq_subscriber& self) { + MONERO_CATCH_AND_RETHROW(self.get_num_connections()); + }) + .def("wait_for_publication", [](monero_zmq_subscriber& self, int timeout_ms) { + MONERO_CATCH_AND_RETHROW(self.wait_for_publication(timeout_ms)); + }, py::arg("timeout_ms"), py::call_guard()) + .def("discard_publications", [](monero_zmq_subscriber& self) { + MONERO_CATCH_AND_RETHROW(self.discard_publications()); + }, py::call_guard()); + // monero_rpc_connection t.py_monero_rpc_connection .def(py::init&>(), py::arg("uri") = "", py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("priority") = 0, py::arg("timeout_ms") = py::none()) @@ -326,6 +347,9 @@ void py_monero_bind_common(py::module_& m, PyMoneroTypes& t) { .def("check_connection", [](monero_rpc_connection& self, const boost::optional& timeout_ms) { MONERO_CATCH_AND_RETHROW(self.check_connection(timeout_ms)); }, py::arg("timeout_ms") = py::none(), py::call_guard()) + .def("subscribe_zmq", [](const monero_rpc_connection& self, const std::vector& topics) { + MONERO_CATCH_AND_RETHROW(std::shared_ptr(self.subscribe_zmq(topics))); + }, py::arg("topics"), py::call_guard()) .def("send_json_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters) { monero_rpc_request request(method, std::make_shared(parameters)); auto response = self.send_json_request(request); diff --git a/src/cpp/py_monero_types.h b/src/cpp/py_monero_types.h index 458871b..9fe610e 100644 --- a/src/cpp/py_monero_types.h +++ b/src/cpp/py_monero_types.h @@ -113,6 +113,8 @@ struct PyMoneroTypes { py::class_> py_monero_rpc_payment_info; py::class_> py_monero_rpc_connection; + py::class_> py_monero_zmq_publication; + py::class_> py_monero_zmq_subscriber; py::class_> py_monero_ssl_options; py::class_> py_monero_version; py::class_> py_monero_block_header; @@ -167,6 +169,8 @@ struct PyMoneroTypes { py_serializable_struct(m, "SerializableStruct"), py_monero_rpc_payment_info(m, "MoneroRpcPaymentInfo"), py_monero_rpc_connection(m, "MoneroRpcConnection"), + py_monero_zmq_publication(m, "MoneroZmqPublication"), + py_monero_zmq_subscriber(m, "MoneroZmqSubscriber"), py_monero_ssl_options(m, "SslOptions"), py_monero_version(m, "MoneroVersion"), py_monero_block_header(m, "MoneroBlockHeader"), diff --git a/src/python/__init__.pyi b/src/python/__init__.pyi index a953b0e..cd44f25 100644 --- a/src/python/__init__.pyi +++ b/src/python/__init__.pyi @@ -118,6 +118,8 @@ from .monero_peer import MoneroPeer from .monero_prune_result import MoneroPruneResult from .monero_rpc_connection import MoneroRpcConnection from .monero_rpc_error import MoneroRpcError +from .monero_zmq_publication import MoneroZmqPublication +from .monero_zmq_subscriber import MoneroZmqSubscriber from .ssl_options import SslOptions from .monero_subaddress import MoneroSubaddress from .monero_submit_tx_result import MoneroSubmitTxResult @@ -204,6 +206,8 @@ __all__ = [ 'MoneroPruneResult', 'MoneroRpcConnection', 'MoneroRpcError', + 'MoneroZmqPublication', + 'MoneroZmqSubscriber', 'MoneroSubaddress', 'MoneroSubmitTxResult', 'MoneroSyncResult', diff --git a/src/python/monero_rpc_connection.pyi b/src/python/monero_rpc_connection.pyi index a8605f2..2de4161 100644 --- a/src/python/monero_rpc_connection.pyi +++ b/src/python/monero_rpc_connection.pyi @@ -1,5 +1,6 @@ import typing +from .monero_zmq_subscriber import MoneroZmqSubscriber from .serializable_struct import SerializableStruct @@ -165,6 +166,16 @@ class MoneroRpcConnection(SerializableStruct): """ ... + def subscribe_zmq(self, topics: list[str]) -> MoneroZmqSubscriber: + """ + Subscribe to the publications of the connection's zmq uri. + + :param list[str] topics: topics to receive, e.g. `json-minimal-chain_main`. + :returns MoneroZmqSubscriber: the subscriber. + :raises MoneroError: if the connection has no zmq uri or the subscription fails. + """ + ... + def send_path_request(self, method: str, parameters: object | None = None, timeout_ms: int | None = None) -> object | None: """ Send a RPC request to the given path and with the given paramters. diff --git a/src/python/monero_zmq_publication.pyi b/src/python/monero_zmq_publication.pyi new file mode 100644 index 0000000..f02bb80 --- /dev/null +++ b/src/python/monero_zmq_publication.pyi @@ -0,0 +1,11 @@ +class MoneroZmqPublication: + """Models a publication of a daemon's zmq publisher.""" + + topic: str + """Publication topic.""" + json: str + """Publication content in JSON format.""" + + def __init__(self) -> None: + """Initialize a new publication.""" + ... diff --git a/src/python/monero_zmq_subscriber.pyi b/src/python/monero_zmq_subscriber.pyi new file mode 100644 index 0000000..a1f60df --- /dev/null +++ b/src/python/monero_zmq_subscriber.pyi @@ -0,0 +1,34 @@ +from .monero_zmq_publication import MoneroZmqPublication + + +class MoneroZmqSubscriber: + """Receives the publications of a daemon's zmq publisher, can only be used by one thread at a time.""" + + def is_connected(self) -> bool: + """ + Indicates if connected to the publisher, which is updated while waiting for publications. + + :returns bool: `True` if connected. + """ + ... + + def get_num_connections(self) -> int: + """ + Get the number of times connected to the publisher, which is updated while waiting for publications. + + :returns int: the number of connections. + """ + ... + + def wait_for_publication(self, timeout_ms: int) -> MoneroZmqPublication | None: + """ + Wait for a publication. + + :param int timeout_ms: the maximum time to wait. + :returns MoneroZmqPublication | None: the publication, or `None` if there is none before the timeout. + """ + ... + + def discard_publications(self) -> None: + """Discard the publications which are pending.""" + ... diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 2ddd247..abeaa9b 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -3,7 +3,6 @@ services: # The dev container is not used, it is just handy to run `docker-compose up dev` to start all services dev: image: alpine:3.22.1 - container_name: dev command: [ "/bin/sh", "-c", "trap : TERM INT; while :; do echo Ready to code and debug like a rockstar!!!; sleep 2073600; done & wait" ] depends_on: - node_2 @@ -13,7 +12,6 @@ services: node_1: image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master} - container_name: node_1 command: [ "--fixed-difficulty=500", "--log-level=3", @@ -42,7 +40,6 @@ services: node_2: image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master} - container_name: node_2 command: [ "--fixed-difficulty=500", "--log-level=3", @@ -78,7 +75,6 @@ services: xmr_wallet_1: image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master} - container_name: xmr_wallet_1 command: [ "--log-level=3", "--allow-mismatched-daemon-version", @@ -104,7 +100,6 @@ services: xmr_wallet_2: image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master} - container_name: xmr_wallet_2 command: [ "--log-level=3", "--allow-mismatched-daemon-version", @@ -130,7 +125,6 @@ services: xmr_wallet_3: image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master} - container_name: xmr_wallet_3 command: [ "--log-level=3", "--allow-mismatched-daemon-version", diff --git a/tests/test_monero_rpc_connection.py b/tests/test_monero_rpc_connection.py index 090ba03..59ac92a 100644 --- a/tests/test_monero_rpc_connection.py +++ b/tests/test_monero_rpc_connection.py @@ -1,7 +1,8 @@ import pytest import logging +import time -from monero import MoneroRpcConnection, MoneroConnectionType, MoneroRpcError, MoneroUtils +from monero import MoneroRpcConnection, MoneroConnectionType, MoneroRpcError, MoneroUtils, MoneroZmqSubscriber from utils import ( TestUtils as Utils, RpcConnectionUtils, StringUtils, BaseTestClass @@ -118,6 +119,24 @@ def test_node_rpc_connection(self, node_connection: MoneroRpcConnection) -> None def test_wallet_rpc_connection(self, wallet_connection: MoneroRpcConnection) -> None: RpcConnectionUtils.test_rpc_connection(wallet_connection, Utils.WALLET_RPC_URI, True, MoneroConnectionType.IPV4) + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + @pytest.mark.skipif(not Utils.DAEMON_RPC_ZMQ_PUB_URI, reason="daemon ZMQ publisher is not configured") + def test_subscribe_zmq(self) -> None: + connection: MoneroRpcConnection = MoneroRpcConnection( + Utils.DAEMON_RPC_URI, + Utils.DAEMON_RPC_USERNAME, + Utils.DAEMON_RPC_PASSWORD, + zmq_uri=Utils.DAEMON_RPC_ZMQ_PUB_URI + ) + subscriber: MoneroZmqSubscriber = connection.subscribe_zmq(["json-minimal-chain_main"]) + + deadline: float = time.monotonic() + 5 + while not subscriber.is_connected() and time.monotonic() < deadline: + subscriber.wait_for_publication(100) + + assert subscriber.is_connected() + assert subscriber.get_num_connections() >= 1 + # Test invalid connection @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") def test_invalid_connection(self) -> None: diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index be7b02e..0551d5a 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -303,7 +303,7 @@ def get_daemon_rpc(cls) -> MoneroDaemonRpc: """ if cls._DAEMON_RPC is None: - cls._DAEMON_RPC = MoneroDaemonRpc(cls.DAEMON_RPC_URI, cls.DAEMON_RPC_USERNAME, cls.DAEMON_RPC_PASSWORD) + cls._DAEMON_RPC = MoneroDaemonRpc(cls.get_daemon_rpc_connection()) return cls._DAEMON_RPC @@ -333,7 +333,12 @@ def get_daemon_rpc_connection(cls) -> MoneroRpcConnection: :returns MoneroRpcConnection: new test daemon rpc connection instance. """ - return MoneroRpcConnection(cls.DAEMON_RPC_URI, cls.DAEMON_RPC_USERNAME, cls.DAEMON_RPC_PASSWORD) + return MoneroRpcConnection( + cls.DAEMON_RPC_URI, + cls.DAEMON_RPC_USERNAME, + cls.DAEMON_RPC_PASSWORD, + zmq_uri=cls.DAEMON_RPC_ZMQ_PUB_URI + ) @classmethod def get_wallet_keys_config(cls) -> MoneroWalletConfig: