From c9b0d11fc4e1a2c23ff94220277f115416255470 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sat, 14 Nov 2026 02:11:15 +0100 Subject: [PATCH] rpc connection: implement zmq listening --- .github/workflows/build.yml | 11 ++ CMakeLists.txt | 29 +++++ src/common/monero_rpc_connection.cpp | 130 ++++++++++++++++++++- src/common/monero_rpc_connection.h | 65 ++++++++++- src/daemon/monero_daemon_rpc.cpp | 168 ++++++++++++++++++++++++--- src/daemon/monero_daemon_rpc.h | 2 +- 6 files changed, 389 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9ab66cb8..84dde5a0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -105,6 +105,17 @@ jobs: cmake --build build cmake --install build + - name: Install zeromq + run: | + # the distro libzmq.a needs gssapi, which has no static archive, so build it without optional dependencies + wget https://github.com/zeromq/libzmq/releases/download/v4.3.5/zeromq-4.3.5.tar.gz + echo "6653ef5910f17954861fe72332e68b03ca6e4d9c7160eb3a8de5a5a913bfab43 zeromq-4.3.5.tar.gz" | sha256sum -c + tar xzf zeromq-4.3.5.tar.gz + cd zeromq-4.3.5 + cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED=OFF -DBUILD_STATIC=ON -DZMQ_BUILD_TESTS=OFF -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DWITH_DOCS=OFF -DWITH_PERF_TOOL=OFF -DENABLE_DRAFTS=OFF -DENABLE_CPACK=OFF -DCMAKE_POLICY_VERSION_MINIMUM=3.5 + cmake --build build -j3 + cmake --install build + - name: Install boost run: | # PIC static boost, same version on all platforms (no distro boost so headers and archives match) diff --git a/CMakeLists.txt b/CMakeLists.txt index 943f30f2..dc7f20b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,6 +170,31 @@ find_path(SODIUM_INCLUDE_DIR sodium/crypto_verify_32.h REQUIRED) include_directories(${SODIUM_INCLUDE_DIR}) message(STATUS "Using libsodium library at ${SODIUM_LIBRARY}") +############ +# libzmq +############ + +# link before libsodium, which static libzmq depends on +find_path(ZMQ_INCLUDE_DIR zmq.h REQUIRED) +find_library(ZMQ_LIBRARY zmq REQUIRED) +include_directories(${ZMQ_INCLUDE_DIR}) +set(ZMQ_LIBRARIES ${ZMQ_LIBRARY}) +if (STATIC) + add_definitions(-DZMQ_STATIC) # as monero-project, else windows expects a zmq dll + # 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}") + ############ # HIDAPI ############ @@ -423,6 +448,7 @@ if (BUILD_LIBRARY) ${Protobuf_LIBRARY} ${LibUSB_LIBRARIES} ${OPENSSL_LIBRARIES} + ${ZMQ_LIBRARIES} ${SODIUM_LIBRARY} ${HIDAPI_LIBRARIES} ${EXTRA_LIBRARIES} @@ -547,6 +573,7 @@ if (BUILD_SAMPLE) ${Protobuf_LIBRARY} ${LibUSB_LIBRARIES} ${OPENSSL_LIBRARIES} + ${ZMQ_LIBRARIES} ${SODIUM_LIBRARY} ${HIDAPI_LIBRARIES} ${EXTRA_LIBRARIES} @@ -619,6 +646,7 @@ if (BUILD_SCRATCHPAD) ${Protobuf_LIBRARY} ${LibUSB_LIBRARIES} ${OPENSSL_LIBRARIES} + ${ZMQ_LIBRARIES} ${SODIUM_LIBRARY} ${HIDAPI_LIBRARIES} ${EXTRA_LIBRARIES} @@ -691,6 +719,7 @@ if (BUILD_TESTS) ${Protobuf_LIBRARY} ${LibUSB_LIBRARIES} ${OPENSSL_LIBRARIES} + ${ZMQ_LIBRARIES} ${SODIUM_LIBRARY} ${HIDAPI_LIBRARIES} ${EXTRA_LIBRARIES} diff --git a/src/common/monero_rpc_connection.cpp b/src/common/monero_rpc_connection.cpp index e4329496..f8aa66d2 100644 --- a/src/common/monero_rpc_connection.cpp +++ b/src/common/monero_rpc_connection.cpp @@ -3,8 +3,12 @@ #include "utils/monero_utils.h" #include "daemon/monero_daemon_rpc_model.h" #include "monero_error.h" +#include "net/net_parse_helpers.h" +#include "net/zmq.h" static const uint32_t DEFAULT_TIMEOUT_MS = 3 * 60 * 1000; // 3 minutes +static const char* ZMQ_MONITOR_ENDPOINT = "inproc://monero_zmq_subscriber_monitor"; +static const int ZMQ_RECONNECT_INTERVAL_MS = 1000; namespace monero { @@ -72,6 +76,115 @@ namespace monero { throw monero_rpc_error(err_code, err_message); } + // --------------------------- MONERO ZMQ SUBSCRIBER --------------------------- + + struct monero_zmq_subscriber::impl { + net::zmq::context m_context; // destroyed after the sockets + net::zmq::socket m_socket; + net::zmq::socket m_monitor; + }; + + monero_zmq_subscriber::monero_zmq_subscriber(const std::string& uri, const std::vector& topics): m_impl(new impl()) { + m_impl->m_context.reset(zmq_ctx_new()); + if (!m_impl->m_context) throw monero_error("Failed to create zmq context: " + net::zmq::get_error_code().message()); + + m_impl->m_socket.reset(zmq_socket(m_impl->m_context.get(), ZMQ_SUB)); + if (!m_impl->m_socket) throw monero_error("Failed to create zmq subscriber: " + net::zmq::get_error_code().message()); + int linger_ms = 0; + int reconnect_ms = ZMQ_RECONNECT_INTERVAL_MS; + zmq_setsockopt(m_impl->m_socket.get(), ZMQ_LINGER, &linger_ms, sizeof(linger_ms)); + zmq_setsockopt(m_impl->m_socket.get(), ZMQ_RECONNECT_IVL, &reconnect_ms, sizeof(reconnect_ms)); + + // connection state comes from a monitor, which must be created before connecting + if (zmq_socket_monitor(m_impl->m_socket.get(), ZMQ_MONITOR_ENDPOINT, ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED) < 0) { + throw monero_error("Failed to monitor zmq subscriber: " + net::zmq::get_error_code().message()); + } + m_impl->m_monitor.reset(zmq_socket(m_impl->m_context.get(), ZMQ_PAIR)); + if (!m_impl->m_monitor) throw monero_error("Failed to create zmq monitor: " + net::zmq::get_error_code().message()); + zmq_setsockopt(m_impl->m_monitor.get(), ZMQ_LINGER, &linger_ms, sizeof(linger_ms)); + if (zmq_connect(m_impl->m_monitor.get(), ZMQ_MONITOR_ENDPOINT) < 0) { + throw monero_error("Failed to connect zmq monitor: " + net::zmq::get_error_code().message()); + } + + for (const std::string& topic : topics) { + if (zmq_setsockopt(m_impl->m_socket.get(), ZMQ_SUBSCRIBE, topic.c_str(), topic.size()) < 0) { + throw monero_error("Failed to subscribe to zmq topic '" + topic + "': " + net::zmq::get_error_code().message()); + } + } + if (zmq_connect(m_impl->m_socket.get(), uri.c_str()) < 0) { + throw monero_error("Failed to connect to zmq uri '" + uri + "': " + net::zmq::get_error_code().message()); + } + } + + monero_zmq_subscriber::~monero_zmq_subscriber() { } + + boost::optional monero_zmq_subscriber::wait_for_publication(int timeout_ms) { + zmq_pollitem_t items[2] = { + { m_impl->m_monitor.get(), 0, ZMQ_POLLIN, 0 }, + { m_impl->m_socket.get(), 0, ZMQ_POLLIN, 0 } + }; + if (zmq_poll(items, 2, timeout_ms) < 0) { + if (zmq_errno() == EINTR) return boost::none; + throw monero_error("Failed to wait for zmq publication: " + net::zmq::get_error_code().message()); + } + if (items[0].revents & ZMQ_POLLIN) read_events(); + if (!(items[1].revents & ZMQ_POLLIN)) return boost::none; + + auto message = net::zmq::receive(m_impl->m_socket.get(), ZMQ_DONTWAIT); + if (!message) { + if (message.error() == net::zmq::make_error_code(EAGAIN)) return boost::none; + throw monero_error("Failed to receive zmq publication: " + message.error().message()); + } + + // "topic:json" + size_t sep = message->find(':'); + if (sep == std::string::npos) return boost::none; + return monero_zmq_publication{ message->substr(0, sep), message->substr(sep + 1) }; + } + + void monero_zmq_subscriber::discard_publications() { + read_events(); + while (true) { + zmq_msg_t msg; + zmq_msg_init(&msg); + int rc = zmq_msg_recv(&msg, m_impl->m_socket.get(), ZMQ_DONTWAIT); + zmq_msg_close(&msg); + if (rc < 0) return; + } + } + + void monero_zmq_subscriber::read_events() { + while (true) { + zmq_msg_t msg; + zmq_msg_init(&msg); + if (zmq_msg_recv(&msg, m_impl->m_monitor.get(), ZMQ_DONTWAIT) < 0) { + zmq_msg_close(&msg); + return; + } + + // event id is in the first frame, the rest is the address + uint16_t event = 0; + if (zmq_msg_size(&msg) >= sizeof(event)) memcpy(&event, zmq_msg_data(&msg), sizeof(event)); + bool more = zmq_msg_more(&msg) != 0; + zmq_msg_close(&msg); + while (more) { + zmq_msg_init(&msg); + if (zmq_msg_recv(&msg, m_impl->m_monitor.get(), ZMQ_DONTWAIT) < 0) { zmq_msg_close(&msg); break; } + more = zmq_msg_more(&msg) != 0; + zmq_msg_close(&msg); + } + + if (event == ZMQ_EVENT_CONNECTED) { + MDEBUG("Connected to zmq publisher"); + m_is_connected = true; + m_num_connections++; + } else if (event == ZMQ_EVENT_DISCONNECTED) { + MDEBUG("Disconnected from zmq publisher"); + m_is_connected = false; + } + } + } + // --------------------------- MONERO RPC CONNECTION --------------------------- bool monero_rpc_connection::compare(int p1, int p2) { @@ -204,6 +317,16 @@ namespace monero { return i->second; } + std::unique_ptr monero_rpc_connection::subscribe_zmq(const std::vector& topics) const { + std::string uri; + { + boost::lock_guard lock(m_mutex); + if (m_zmq_uri == boost::none || m_zmq_uri.get().empty()) throw monero_error("Connection has no zmq uri"); + uri = m_zmq_uri.get(); + } + return std::unique_ptr(new monero_zmq_subscriber(uri, topics)); + } + boost::optional monero_rpc_connection::is_online() const { boost::lock_guard lock(m_mutex); return m_is_online; @@ -355,6 +478,11 @@ namespace monero { if (!m_http_client->set_server(uri, login, std::move(ssl))) throw monero_error("Could not set uri: " + uri); if (!m_http_client->connect(std::chrono::milliseconds(DEFAULT_TIMEOUT_MS))) MWARNING("Could not connect to server: " + uri); + // the http client only keeps host and port, so requests are sent under the path of the uri + epee::net_utils::http::url_content parsed; + m_path_prefix = epee::net_utils::parse_url(uri, parsed) ? parsed.m_uri_content.m_path : ""; + while (!m_path_prefix.empty() && m_path_prefix.back() == '/') m_path_prefix.pop_back(); + m_applied = key; } @@ -373,7 +501,7 @@ namespace monero { // invoke http json const uint64_t received_before = m_http_client->get_bytes_received(); - if (!m_http_client->invoke_post(uri, body, std::chrono::milliseconds(timeout), std::addressof(pri))) { + if (!m_http_client->invoke_post(m_path_prefix + uri.to_string(), body, std::chrono::milliseconds(timeout), std::addressof(pri))) { // epee never surfaces the 401 status, but a rejected login means the server answered; // a dead or idle-closed socket yields no response bytes at all diff --git a/src/common/monero_rpc_connection.h b/src/common/monero_rpc_connection.h index 35886e88..0613fad8 100644 --- a/src/common/monero_rpc_connection.h +++ b/src/common/monero_rpc_connection.h @@ -104,6 +104,59 @@ namespace monero { boost::optional m_binary; }; + /** + * Models a publication of a Monero daemon's zmq publisher. + */ + struct monero_zmq_publication { + std::string m_topic; + std::string m_json; + }; + + /** + * Receives the publications of a Monero daemon's zmq publisher, can only be used by one thread at a time. + */ + class monero_zmq_subscriber { + public: + ~monero_zmq_subscriber(); + + /** + * Indicates if connected to the publisher, which is updated while waiting for publications. + * + * @return true if connected + */ + bool is_connected() const { return m_is_connected; } + + /** + * Get the number of times connected to the publisher, which is updated while waiting for publications. + * + * @return the number of connections + */ + uint64_t get_num_connections() const { return m_num_connections; } + + /** + * Wait for a publication. + * + * @param timeout_ms the maximum time to wait + * @return the publication, or none if there is none before the timeout + */ + boost::optional wait_for_publication(int timeout_ms); + + /** + * Discard the publications which are pending. + */ + void discard_publications(); + + private: + friend class monero_rpc_connection; + struct impl; + std::unique_ptr m_impl; + bool m_is_connected = false; + uint64_t m_num_connections = 0; + + monero_zmq_subscriber(const std::string& uri, const std::vector& topics); + void read_events(); + }; + /** * Maintains a connection and sends requests to a Monero RPC API. */ @@ -114,7 +167,7 @@ namespace monero { boost::optional m_password; boost::optional m_proxy_uri; bool m_ssl_verify = true; // verify TLS certificates and hostnames - boost::optional m_zmq_uri; // TODO: implement zmq listener + boost::optional m_zmq_uri; // monerod/monero-wallet-rpc --zmq-pub uri boost::optional m_timeout_ms; // RPC request timeout in milliseconds boost::optional m_response_time; // automatically set by calling check_connection() int m_priority; // priority relative to other connections where 1 is highest, then 2, etc, and 0 is lowest (default) @@ -217,6 +270,15 @@ namespace monero { */ bool check_connection(const boost::optional& timeout_ms = boost::none); + /** + * Subscribe to the publications of the connection's zmq uri. + * + * @param topics are the topics to receive, e.g. "json-minimal-chain_main" + * @return the subscriber + * @throws if the connection has no zmq uri or the subscription fails + */ + std::unique_ptr subscribe_zmq(const std::vector& topics) const; + /** * Send a request to the RPC API. * @@ -274,6 +336,7 @@ namespace monero { mutable std::unique_ptr m_http_client; std::unordered_map m_attributes; mutable std::tuple m_applied{"", "", "", "", true}; + mutable std::string m_path_prefix; // path of the uri, prepended to request paths (e.g. lws rest server prefix) void ensure_configured() const; std::string invoke_post(const boost::string_ref uri, const std::string& body, const boost::optional& timeout_ms = boost::none) const; diff --git a/src/daemon/monero_daemon_rpc.cpp b/src/daemon/monero_daemon_rpc.cpp index dc5e4f22..74458797 100644 --- a/src/daemon/monero_daemon_rpc.cpp +++ b/src/daemon/monero_daemon_rpc.cpp @@ -56,6 +56,8 @@ #include "common/monero_error.h" #include "utils/gen_utils.h" #include "utils/monero_utils.h" +#include "serialization/keyvalue_serialization.h" +#include "storages/portable_storage_template_helper.h" static const uint64_t MAX_REQ_SIZE = 3000000; static const uint64_t NUM_HEADERS_PER_REQ = 750; @@ -63,36 +65,173 @@ static const uint64_t NUM_HEADERS_PER_REQ = 750; namespace monero { /** - * Polls daemon and sends notifications in order to notify external daemon listeners. + * Payload of monerod's "json-minimal-chain_main" zmq publication. + */ + struct zmq_chain_main { + std::vector ids; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(ids) + END_KV_SERIALIZE_MAP() + }; + + /** + * Notifies daemon listeners of new blocks from the daemon's zmq publications (monerod --zmq-pub), + * polling the daemon if zmq is not configured or not connected. */ class monero_daemon_poller : public gen_utils::thread_poller { public: - explicit monero_daemon_poller(monero_daemon* daemon, uint64_t poll_period_ms = 10000): m_daemon(daemon) { + explicit monero_daemon_poller(monero_daemon* daemon, const std::shared_ptr& rpc, uint64_t poll_period_ms = 10000): + m_daemon(daemon), m_rpc(rpc), m_daemon_poll_period_ms(poll_period_ms) { init_common("monero_daemon_rpc"); m_poll_period_ms = poll_period_ms; + set_zmq_uri(rpc->m_zmq_uri); } ~monero_daemon_poller() override { set_is_polling(false); } + void set_daemon_poll_period_in_ms(uint64_t period_ms) { + m_daemon_poll_period_ms = period_ms; + } + + void set_zmq_uri(const boost::optional& zmq_uri) { + std::string uri = zmq_uri == boost::none ? "" : zmq_uri.get(); + boost::lock_guard lock(m_zmq_uri_mutex); + if (uri == m_zmq_uri) return; + m_zmq_uri = uri; + m_zmq_changed = true; + } + + void request_listening(bool listening) { + if (!listening) m_listening_stopped = true; + request_is_polling(listening); + } + void poll() override { - if (!m_last_header) { - m_last_header = m_daemon->get_last_block_header(); + if (m_zmq_changed.exchange(false)) configure_zmq(); + try { + poll_blocks(); + } catch (...) { + update_poll_period(); + throw; + } + update_poll_period(); + } + + private: + static constexpr const char* ZMQ_CHAIN_MAIN_TOPIC = "json-minimal-chain_main"; + static constexpr int ZMQ_WAIT_TIMEOUT_MS = 200; + + monero_daemon* m_daemon; + std::shared_ptr m_rpc; + std::atomic m_daemon_poll_period_ms; + std::shared_ptr m_last_header; + std::chrono::steady_clock::time_point m_last_daemon_poll; + + // guarded by m_zmq_uri_mutex + boost::mutex m_zmq_uri_mutex; + std::string m_zmq_uri; + std::atomic m_zmq_changed{false}; + std::atomic m_listening_stopped{false}; + + // zmq state, poll thread only + std::unique_ptr m_zmq; + uint64_t m_zmq_connections = 0; + bool m_catch_up = true; // poll the daemon for blocks missed while zmq was not receiving + + // poll() waits for zmq publications itself + void update_poll_period() { + m_poll_period_ms = m_zmq ? 0 : m_daemon_poll_period_ms.load(); + } + + void configure_zmq() { + m_zmq.reset(); + m_zmq_connections = 0; + m_catch_up = true; + + std::string uri; + { + boost::lock_guard lock(m_zmq_uri_mutex); + uri = m_zmq_uri; + } + if (uri.empty()) return; + + try { + m_zmq = m_rpc->subscribe_zmq({ ZMQ_CHAIN_MAIN_TOPIC }); + } catch (const std::exception& e) { + MWARNING("Could not receive blocks from zmq uri '" << uri << "', polling the daemon instead: " << e.what()); + } + } + + void poll_blocks() { + if (m_listening_stopped.exchange(false) && m_zmq) { + // discard what was published while not listening + m_zmq->discard_publications(); + m_catch_up = true; + } + + if (!m_zmq) { + poll_daemon(); + return; + } + + boost::optional publication; + try { + publication = m_zmq->wait_for_publication(ZMQ_WAIT_TIMEOUT_MS); + } catch (const std::exception& e) { + MWARNING("Could not receive from zmq, polling the daemon instead: " << e.what()); + m_zmq.reset(); + poll_daemon(); return; } + // catch up with the blocks added while disconnected + if (m_zmq->get_num_connections() != m_zmq_connections) { + m_zmq_connections = m_zmq->get_num_connections(); + m_catch_up = true; + } + if (publication) announce_published_tip(publication.get()); + + // poll the daemon while zmq is not connected + bool poll_due = std::chrono::steady_clock::now() - m_last_daemon_poll >= std::chrono::milliseconds(m_daemon_poll_period_ms.load()); + if (poll_due && (!m_zmq->is_connected() || m_catch_up)) poll_daemon(); + } + + void poll_daemon() { + m_last_daemon_poll = std::chrono::steady_clock::now(); auto header = m_daemon->get_last_block_header(); - if (header->m_hash != m_last_header->m_hash) { + m_catch_up = false; + if (!m_last_header) { m_last_header = header; - announce_block_header(header); + return; } + announce_if_new(header); } - private: - monero_daemon* m_daemon; - std::shared_ptr m_last_header; + void announce_published_tip(const monero_zmq_publication& publication) { + zmq_chain_main chain_main; + if (!epee::serialization::load_t_from_json(chain_main, publication.m_json) || chain_main.ids.empty()) { + MWARNING("Could not parse zmq publication: " << publication.m_json.substr(0, 256)); + return; + } + + // notify the new tip as when polling + try { + announce_if_new(m_daemon->get_block_header_by_hash(chain_main.ids.back())); + } catch (const std::exception& e) { + MWARNING("Could not fetch header of block published by zmq, polling the daemon: " << e.what()); + m_catch_up = true; + } + } + + void announce_if_new(const std::shared_ptr& header) { + if (m_last_header && header->m_hash == m_last_header->m_hash) return; + m_last_header = header; + announce_block_header(header); + } void announce_block_header(const std::shared_ptr& header) { gen_utils::thread_poller::announce_scope scope(*this); // see wait_for_callbacks_idle() @@ -129,8 +268,8 @@ namespace monero { void monero_daemon_rpc::set_poll_period_in_ms(uint64_t period_ms) { boost::lock_guard poller_lock(m_poller_mutex); - if (!m_poller) m_poller = std::make_unique(this); - m_poller->set_period_in_ms(period_ms); + if (!m_poller) m_poller = std::make_unique(this, m_rpc); + m_poller->set_daemon_poll_period_in_ms(period_ms); } /** @@ -1034,9 +1173,12 @@ namespace monero { } if (!m_poller && should_poll) { - m_poller = std::make_unique(this); + m_poller = std::make_unique(this, m_rpc); + } + if (m_poller) { + m_poller->set_zmq_uri(m_rpc->m_zmq_uri); + m_poller->request_listening(should_poll); } - if (m_poller) m_poller->request_is_polling(should_poll); } monero_daemon_rpc::~monero_daemon_rpc() { diff --git a/src/daemon/monero_daemon_rpc.h b/src/daemon/monero_daemon_rpc.h index e34347b2..86062ea1 100644 --- a/src/daemon/monero_daemon_rpc.h +++ b/src/daemon/monero_daemon_rpc.h @@ -91,7 +91,7 @@ namespace monero { bool is_connected(); /** - * Set the daemon poll period. + * Set the daemon poll period, used when zmq is not configured or not connected. * * @param period_ms poll period in milliseconds. */