Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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
############
Expand Down Expand Up @@ -375,6 +402,7 @@ set(MONERO_PYTHON_LINK_LIBS
${OPENSSL_LIBRARIES}
${Protobuf_LIBRARY}
${LibUSB_LIBRARIES}
${ZMQ_LIBRARIES}
${SODIUM_LIBRARY}
${HIDAPI_LIBRARIES}
${UNBOUND_LIBRARIES}
Expand Down
24 changes: 24 additions & 0 deletions src/cpp/common/py_monero_common_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<py::gil_scoped_release>())
.def("discard_publications", [](monero_zmq_subscriber& self) {
MONERO_CATCH_AND_RETHROW(self.discard_publications());
}, py::call_guard<py::gil_scoped_release>());

// monero_rpc_connection
t.py_monero_rpc_connection
.def(py::init<const std::string&, const std::string&, const::std::string&, const std::string&, const std::string&, int, const boost::optional<uint32_t>&>(), 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())
Expand Down Expand Up @@ -326,6 +347,9 @@ void py_monero_bind_common(py::module_& m, PyMoneroTypes& t) {
.def("check_connection", [](monero_rpc_connection& self, const boost::optional<uint32_t>& timeout_ms) {
MONERO_CATCH_AND_RETHROW(self.check_connection(timeout_ms));
}, py::arg("timeout_ms") = py::none(), py::call_guard<py::gil_scoped_release>())
.def("subscribe_zmq", [](const monero_rpc_connection& self, const std::vector<std::string>& topics) {
MONERO_CATCH_AND_RETHROW(std::shared_ptr<monero_zmq_subscriber>(self.subscribe_zmq(topics)));
}, py::arg("topics"), py::call_guard<py::gil_scoped_release>())
.def("send_json_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional<py::object>& parameters) {
monero_rpc_request request(method, std::make_shared<PyMoneroRequestParams>(parameters));
auto response = self.send_json_request(request);
Expand Down
4 changes: 4 additions & 0 deletions src/cpp/py_monero_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ struct PyMoneroTypes {
py::class_<monero_rpc_payment_info, serializable_struct, std::shared_ptr<monero_rpc_payment_info>> py_monero_rpc_payment_info;
py::class_<monero_rpc_connection, serializable_struct, std::shared_ptr<monero_rpc_connection>> py_monero_rpc_connection;

py::class_<monero_zmq_publication, std::shared_ptr<monero_zmq_publication>> py_monero_zmq_publication;
py::class_<monero_zmq_subscriber, std::shared_ptr<monero_zmq_subscriber>> py_monero_zmq_subscriber;
py::class_<ssl_options, serializable_struct, std::shared_ptr<ssl_options>> py_monero_ssl_options;
py::class_<monero_version, serializable_struct, std::shared_ptr<monero_version>> py_monero_version;
py::class_<monero_block_header, serializable_struct, std::shared_ptr<monero_block_header>> py_monero_block_header;
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions src/python/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,6 +206,8 @@ __all__ = [
'MoneroPruneResult',
'MoneroRpcConnection',
'MoneroRpcError',
'MoneroZmqPublication',
'MoneroZmqSubscriber',
'MoneroSubaddress',
'MoneroSubmitTxResult',
'MoneroSyncResult',
Expand Down
11 changes: 11 additions & 0 deletions src/python/monero_rpc_connection.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import typing

from .monero_zmq_subscriber import MoneroZmqSubscriber
from .serializable_struct import SerializableStruct


Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/python/monero_zmq_publication.pyi
Original file line number Diff line number Diff line change
@@ -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."""
...
34 changes: 34 additions & 0 deletions src/python/monero_zmq_subscriber.pyi
Original file line number Diff line number Diff line change
@@ -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."""
...
6 changes: 0 additions & 6 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
21 changes: 20 additions & 1 deletion tests/test_monero_rpc_connection.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
Loading