|
| 1 | +// dashd ZMQ `hashblock` instant tip-notify -- implementation. |
| 2 | +// See zmq_tip_notify.hpp for the contract. libzmq usage is guarded by |
| 3 | +// C2POOL_ZMQ; the pure hex helper is always compiled so the KAT can pin the |
| 4 | +// frame-decode contract without libzmq. |
| 5 | + |
| 6 | +#include "zmq_tip_notify.hpp" |
| 7 | + |
| 8 | +// NOTE: all #includes MUST be at file scope, never inside the namespace below. |
| 9 | +#ifdef C2POOL_ZMQ |
| 10 | +#include <zmq.h> |
| 11 | +#include <cerrno> |
| 12 | +#include <chrono> |
| 13 | +#include <cstring> |
| 14 | +#include <iostream> |
| 15 | +#include <thread> |
| 16 | +#include <utility> |
| 17 | +#endif |
| 18 | + |
| 19 | +namespace dash::coin { |
| 20 | + |
| 21 | +std::string zmq_hashblock_frame_to_hex(const unsigned char* body, unsigned long len) |
| 22 | +{ |
| 23 | + if (!body || len != 32) |
| 24 | + return {}; |
| 25 | + static const char* const hexd = "0123456789abcdef"; |
| 26 | + std::string out; |
| 27 | + out.resize(64); |
| 28 | + // dashd publishes the hash ALREADY in RPC/display (reversed) byte order: |
| 29 | + // CZMQPublishHashBlockNotifier::NotifyBlock writes data[31-i]=hash.begin()[i] |
| 30 | + // before SendZmqMessage, i.e. it reverses the internal little-endian array |
| 31 | + // for us (Bitcoin Core doc/zmq.md: hashes are published "in reversed byte |
| 32 | + // order, the same format as the RPC interface"). So the wire frame |
| 33 | + // hex-encodes DIRECTLY to the getbestblockhash string -- do NOT reverse |
| 34 | + // again, or the ZMQ hash never matches the poll's and the shared |
| 35 | + // TipHashDedup never coalesces the poll+ZMQ double-fire. |
| 36 | + for (unsigned i = 0; i < 32; ++i) { |
| 37 | + const unsigned char b = body[i]; |
| 38 | + out[i * 2] = hexd[b >> 4]; |
| 39 | + out[i * 2 + 1] = hexd[b & 0x0f]; |
| 40 | + } |
| 41 | + return out; |
| 42 | +} |
| 43 | + |
| 44 | +#ifdef C2POOL_ZMQ |
| 45 | + |
| 46 | +ZmqHashblockSubscriber::ZmqHashblockSubscriber( |
| 47 | + std::string endpoint, std::function<void(const std::string&)> on_new_tip) |
| 48 | + : endpoint_(std::move(endpoint)), on_new_tip_(std::move(on_new_tip)) |
| 49 | +{ |
| 50 | +} |
| 51 | + |
| 52 | +ZmqHashblockSubscriber::~ZmqHashblockSubscriber() |
| 53 | +{ |
| 54 | + stop(); |
| 55 | +} |
| 56 | + |
| 57 | +void ZmqHashblockSubscriber::start() |
| 58 | +{ |
| 59 | + if (running_.load() || thread_.joinable()) |
| 60 | + return; |
| 61 | + ctx_ = zmq_ctx_new(); |
| 62 | + if (!ctx_) { |
| 63 | + std::cout << "[Stratum] zmq hashblock: zmq_ctx_new failed; " |
| 64 | + "falling back to the 3 s poll only\n"; |
| 65 | + return; |
| 66 | + } |
| 67 | + stop_.store(false); |
| 68 | + thread_ = std::thread([this] { run_loop(); }); |
| 69 | +} |
| 70 | + |
| 71 | +void ZmqHashblockSubscriber::stop() |
| 72 | +{ |
| 73 | + stop_.store(true); |
| 74 | + if (thread_.joinable()) |
| 75 | + thread_.join(); |
| 76 | + if (ctx_) { |
| 77 | + zmq_ctx_term(ctx_); |
| 78 | + ctx_ = nullptr; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void ZmqHashblockSubscriber::run_loop() |
| 83 | +{ |
| 84 | + running_.store(true); |
| 85 | + while (!stop_.load()) { |
| 86 | + void* sub = zmq_socket(ctx_, ZMQ_SUB); |
| 87 | + if (!sub) { |
| 88 | + // Cannot create a socket -- back off and retry (never spin). |
| 89 | + for (int i = 0; i < 20 && !stop_.load(); ++i) |
| 90 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 91 | + continue; |
| 92 | + } |
| 93 | + int rcv_timeout_ms = 500; // so the loop re-checks stop_ ~2x/s |
| 94 | + zmq_setsockopt(sub, ZMQ_RCVTIMEO, &rcv_timeout_ms, sizeof rcv_timeout_ms); |
| 95 | + int linger = 0; |
| 96 | + zmq_setsockopt(sub, ZMQ_LINGER, &linger, sizeof linger); |
| 97 | + zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "hashblock", 9); |
| 98 | + |
| 99 | + if (zmq_connect(sub, endpoint_.c_str()) != 0) { |
| 100 | + std::cout << "[Stratum] zmq hashblock: connect to " << endpoint_ |
| 101 | + << " failed (" << zmq_strerror(zmq_errno()) |
| 102 | + << "); the 3 s poll remains the active path; retrying\n"; |
| 103 | + zmq_close(sub); |
| 104 | + for (int i = 0; i < 20 && !stop_.load(); ++i) |
| 105 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 106 | + continue; |
| 107 | + } |
| 108 | + std::cout << "[Stratum] zmq hashblock: subscribed at " << endpoint_ |
| 109 | + << " (primary instant tip-notify; 3 s poll is the backstop)\n"; |
| 110 | + |
| 111 | + // Receive loop. dashd hashblock messages are multipart: |
| 112 | + // [0] topic "hashblock" |
| 113 | + // [1] body 32-byte block hash (little-endian internal order) |
| 114 | + // [2] seq 4-byte LE sequence (drained, unused) |
| 115 | + while (!stop_.load()) { |
| 116 | + zmq_msg_t topic; |
| 117 | + zmq_msg_init(&topic); |
| 118 | + int r = zmq_msg_recv(&topic, sub, 0); |
| 119 | + if (r < 0) { |
| 120 | + zmq_msg_close(&topic); |
| 121 | + if (zmq_errno() == EAGAIN) |
| 122 | + continue; // timeout: re-check stop_ and retry |
| 123 | + break; // real transport error: reconnect |
| 124 | + } |
| 125 | + std::string hex; |
| 126 | + int more = 0; |
| 127 | + size_t more_sz = sizeof more; |
| 128 | + zmq_getsockopt(sub, ZMQ_RCVMORE, &more, &more_sz); |
| 129 | + if (more) { |
| 130 | + zmq_msg_t body; |
| 131 | + zmq_msg_init(&body); |
| 132 | + if (zmq_msg_recv(&body, sub, 0) >= 0) { |
| 133 | + hex = zmq_hashblock_frame_to_hex( |
| 134 | + static_cast<const unsigned char*>(zmq_msg_data(&body)), |
| 135 | + zmq_msg_size(&body)); |
| 136 | + } |
| 137 | + zmq_msg_close(&body); |
| 138 | + } |
| 139 | + zmq_msg_close(&topic); |
| 140 | + // Drain any trailing frames (sequence number etc). |
| 141 | + more = 0; |
| 142 | + more_sz = sizeof more; |
| 143 | + zmq_getsockopt(sub, ZMQ_RCVMORE, &more, &more_sz); |
| 144 | + while (more && !stop_.load()) { |
| 145 | + zmq_msg_t drop; |
| 146 | + zmq_msg_init(&drop); |
| 147 | + zmq_msg_recv(&drop, sub, 0); |
| 148 | + zmq_msg_close(&drop); |
| 149 | + more = 0; |
| 150 | + more_sz = sizeof more; |
| 151 | + zmq_getsockopt(sub, ZMQ_RCVMORE, &more, &more_sz); |
| 152 | + } |
| 153 | + if (!hex.empty() && on_new_tip_) { |
| 154 | + try { |
| 155 | + on_new_tip_(hex); |
| 156 | + } catch (...) { |
| 157 | + // never let a callback throw kill the subscriber thread |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + zmq_close(sub); |
| 162 | + // Bounded backoff before reconnect on a broken stream. |
| 163 | + for (int i = 0; i < 20 && !stop_.load(); ++i) |
| 164 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 165 | + } |
| 166 | + running_.store(false); |
| 167 | +} |
| 168 | + |
| 169 | +#endif // C2POOL_ZMQ |
| 170 | + |
| 171 | +} // namespace dash::coin |
0 commit comments