Branch data Line data Source code
1 : : // Copyright (c) 2015-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <zmq/zmqpublishnotifier.h>
6 : :
7 : : #include <chain.h>
8 : : #include <crypto/common.h>
9 : : #include <logging.h>
10 : : #include <netaddress.h>
11 : : #include <netbase.h>
12 : : #include <primitives/transaction.h>
13 : : #include <serialize.h>
14 : : #include <streams.h>
15 : : #include <uint256.h>
16 : : #include <util/check.h>
17 : : #include <zmq/zmqutil.h>
18 : :
19 : : #include <zmq.h>
20 : :
21 : : #include <cstdarg>
22 : : #include <cstddef>
23 : : #include <cstdint>
24 : : #include <cstring>
25 : : #include <map>
26 : : #include <optional>
27 : : #include <string>
28 : : #include <utility>
29 : : #include <vector>
30 : :
31 : : static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifiers;
32 : :
33 : : static const char *MSG_HASHBLOCK = "hashblock";
34 : : static const char *MSG_HASHTX = "hashtx";
35 : : static const char *MSG_RAWBLOCK = "rawblock";
36 : : static const char *MSG_RAWTX = "rawtx";
37 : : static const char *MSG_SEQUENCE = "sequence";
38 : :
39 : : // Internal function to send multipart message
40 : 127 : static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
41 : : {
42 : 127 : va_list args;
43 : 127 : va_start(args, size);
44 : :
45 : 635 : while (1)
46 : : {
47 : 381 : zmq_msg_t msg;
48 : :
49 : 381 : int rc = zmq_msg_init_size(&msg, size);
50 [ - + ]: 381 : if (rc != 0)
51 : : {
52 [ # # ]: 0 : zmqError("Unable to initialize ZMQ msg");
53 : 0 : va_end(args);
54 : 0 : return -1;
55 : : }
56 : :
57 : 381 : void *buf = zmq_msg_data(&msg);
58 : 381 : memcpy(buf, data, size);
59 : :
60 : 381 : data = va_arg(args, const void*);
61 : :
62 [ + + ]: 508 : rc = zmq_msg_send(&msg, sock, data ? ZMQ_SNDMORE : 0);
63 [ - + ]: 381 : if (rc == -1)
64 : : {
65 [ # # ]: 0 : zmqError("Unable to send ZMQ msg");
66 : 0 : zmq_msg_close(&msg);
67 : 0 : va_end(args);
68 : 0 : return -1;
69 : : }
70 : :
71 : 381 : zmq_msg_close(&msg);
72 : :
73 [ + + ]: 381 : if (!data)
74 : : break;
75 : :
76 : 254 : size = va_arg(args, size_t);
77 : 254 : }
78 : 127 : va_end(args);
79 : 127 : return 0;
80 : : }
81 : :
82 : 9 : static bool IsZMQAddressIPV6(const std::string &zmq_address)
83 : : {
84 : 9 : const std::string tcp_prefix = "tcp://";
85 [ - + ]: 9 : const size_t tcp_index = zmq_address.rfind(tcp_prefix);
86 : 9 : const size_t colon_index = zmq_address.rfind(':');
87 [ + + ]: 9 : if (tcp_index == 0 && colon_index != std::string::npos) {
88 [ + - ]: 6 : const std::string ip = zmq_address.substr(tcp_prefix.length(), colon_index - tcp_prefix.length());
89 [ + - + - ]: 6 : const std::optional<CNetAddr> addr{LookupHost(ip, false)};
90 [ + - - + ]: 6 : if (addr.has_value() && addr.value().IsIPv6()) return true;
91 : 6 : }
92 : : return false;
93 : 9 : }
94 : :
95 : 16 : bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
96 : : {
97 [ - + ]: 16 : assert(!psocket);
98 : :
99 : : // check if address is being used by other publish notifier
100 : 16 : std::multimap<std::string, CZMQAbstractPublishNotifier*>::iterator i = mapPublishNotifiers.find(address);
101 : :
102 [ + + ]: 16 : if (i==mapPublishNotifiers.end())
103 : : {
104 : 9 : psocket = zmq_socket(pcontext, ZMQ_PUB);
105 [ - + ]: 9 : if (!psocket)
106 : : {
107 [ # # ]: 0 : zmqError("Failed to create socket");
108 : 0 : return false;
109 : : }
110 : :
111 [ + - ]: 9 : LogDebug(BCLog::ZMQ, "Outbound message high water mark for %s at %s is %d\n", type, address, outbound_message_high_water_mark);
112 : :
113 : 9 : int rc = zmq_setsockopt(psocket, ZMQ_SNDHWM, &outbound_message_high_water_mark, sizeof(outbound_message_high_water_mark));
114 [ - + ]: 9 : if (rc != 0)
115 : : {
116 [ # # ]: 0 : zmqError("Failed to set outbound message high water mark");
117 : 0 : zmq_close(psocket);
118 : 0 : return false;
119 : : }
120 : :
121 : 9 : const int so_keepalive_option {1};
122 : 9 : rc = zmq_setsockopt(psocket, ZMQ_TCP_KEEPALIVE, &so_keepalive_option, sizeof(so_keepalive_option));
123 [ - + ]: 9 : if (rc != 0) {
124 [ # # ]: 0 : zmqError("Failed to set SO_KEEPALIVE");
125 : 0 : zmq_close(psocket);
126 : 0 : return false;
127 : : }
128 : :
129 : : // On some systems (e.g. OpenBSD) the ZMQ_IPV6 must not be enabled, if the address to bind isn't IPv6
130 [ + - ]: 9 : const int enable_ipv6 { IsZMQAddressIPV6(address) ? 1 : 0};
131 : 9 : rc = zmq_setsockopt(psocket, ZMQ_IPV6, &enable_ipv6, sizeof(enable_ipv6));
132 [ - + ]: 9 : if (rc != 0) {
133 [ # # ]: 0 : zmqError("Failed to set ZMQ_IPV6");
134 : 0 : zmq_close(psocket);
135 : 0 : return false;
136 : : }
137 : :
138 : 9 : rc = zmq_bind(psocket, address.c_str());
139 [ + + ]: 9 : if (rc != 0)
140 : : {
141 [ + - ]: 2 : zmqError("Failed to bind address");
142 : 2 : zmq_close(psocket);
143 : 2 : return false;
144 : : }
145 : :
146 : : // register this notifier for the address, so it can be reused for other publish notifier
147 [ - + + - ]: 14 : mapPublishNotifiers.insert(std::make_pair(address, this));
148 : 7 : return true;
149 : : }
150 : : else
151 : : {
152 [ + - ]: 7 : LogDebug(BCLog::ZMQ, "Reusing socket for address %s\n", address);
153 [ + - ]: 7 : LogDebug(BCLog::ZMQ, "Outbound message high water mark for %s at %s is %d\n", type, address, outbound_message_high_water_mark);
154 : :
155 [ - + ]: 7 : psocket = i->second->psocket;
156 [ - + + - ]: 14 : mapPublishNotifiers.insert(std::make_pair(address, this));
157 : :
158 : 7 : return true;
159 : : }
160 : : }
161 : :
162 : 18 : void CZMQAbstractPublishNotifier::Shutdown()
163 : : {
164 : : // Early return if Initialize was not called
165 [ + + ]: 18 : if (!psocket) return;
166 : :
167 : 16 : int count = mapPublishNotifiers.count(address);
168 : :
169 : : // remove this notifier from the list of publishers using this address
170 : 16 : typedef std::multimap<std::string, CZMQAbstractPublishNotifier*>::iterator iterator;
171 : 16 : std::pair<iterator, iterator> iterpair = mapPublishNotifiers.equal_range(address);
172 : :
173 [ + + ]: 16 : for (iterator it = iterpair.first; it != iterpair.second; ++it)
174 : : {
175 [ + - ]: 14 : if (it->second==this)
176 : : {
177 : 14 : mapPublishNotifiers.erase(it);
178 : 14 : break;
179 : : }
180 : : }
181 : :
182 [ + + ]: 16 : if (count == 1)
183 : : {
184 [ + - ]: 7 : LogDebug(BCLog::ZMQ, "Close socket at address %s\n", address);
185 : 7 : int linger = 0;
186 : 7 : zmq_setsockopt(psocket, ZMQ_LINGER, &linger, sizeof(linger));
187 : 7 : zmq_close(psocket);
188 : : }
189 : :
190 : 16 : psocket = nullptr;
191 : : }
192 : :
193 : 127 : bool CZMQAbstractPublishNotifier::SendZmqMessage(const char *command, const void* data, size_t size)
194 : : {
195 [ - + ]: 127 : assert(psocket);
196 : :
197 : : /* send three parts, command & data & a LE 4byte sequence number */
198 : 127 : unsigned char msgseq[sizeof(uint32_t)];
199 : 127 : WriteLE32(msgseq, nSequence);
200 : 127 : int rc = zmq_send_multipart(psocket, command, strlen(command), data, size, msgseq, (size_t)sizeof(uint32_t), nullptr);
201 [ + - ]: 127 : if (rc == -1)
202 : : return false;
203 : :
204 : : /* increment memory only sequence number after sending */
205 : 127 : nSequence++;
206 : :
207 : 127 : return true;
208 : : }
209 : :
210 : 22 : bool CZMQPublishHashBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
211 : : {
212 : 22 : uint256 hash = pindex->GetBlockHash();
213 [ + - + - ]: 44 : LogDebug(BCLog::ZMQ, "Publish hashblock %s to %s\n", hash.GetHex(), this->address);
214 : : uint8_t data[32];
215 [ + + ]: 726 : for (unsigned int i = 0; i < 32; i++) {
216 : 704 : data[31 - i] = hash.begin()[i];
217 : : }
218 : 22 : return SendZmqMessage(MSG_HASHBLOCK, data, 32);
219 : : }
220 : :
221 : 31 : bool CZMQPublishHashTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
222 : : {
223 : 31 : uint256 hash = transaction.GetHash().ToUint256();
224 [ + - + - ]: 62 : LogDebug(BCLog::ZMQ, "Publish hashtx %s to %s\n", hash.GetHex(), this->address);
225 : : uint8_t data[32];
226 [ + + ]: 1023 : for (unsigned int i = 0; i < 32; i++) {
227 : 992 : data[31 - i] = hash.begin()[i];
228 : : }
229 : 31 : return SendZmqMessage(MSG_HASHTX, data, 32);
230 : : }
231 : :
232 : 14 : bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
233 : : {
234 [ + - + - ]: 28 : LogDebug(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address);
235 : :
236 : 14 : std::vector<std::byte> block{};
237 [ + - - + ]: 14 : if (!m_get_block_by_index(block, *pindex)) {
238 [ # # # # ]: 0 : zmqError("Can't read block from disk");
239 : 0 : return false;
240 : : }
241 : :
242 [ - + + - ]: 14 : return SendZmqMessage(MSG_RAWBLOCK, block.data(), block.size());
243 : 14 : }
244 : :
245 : 18 : bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
246 : : {
247 : 18 : uint256 hash = transaction.GetHash().ToUint256();
248 [ + - + - ]: 36 : LogDebug(BCLog::ZMQ, "Publish rawtx %s to %s\n", hash.GetHex(), this->address);
249 : 18 : DataStream ss;
250 [ + - ]: 18 : ss << TX_WITH_WITNESS(transaction);
251 [ - + + - ]: 18 : return SendZmqMessage(MSG_RAWTX, &(*ss.begin()), ss.size());
252 : 18 : }
253 : :
254 : : // Helper function to send a 'sequence' topic message with the following structure:
255 : : // <32-byte hash> | <1-byte label> | <8-byte LE sequence> (optional)
256 : 42 : static bool SendSequenceMsg(CZMQAbstractPublishNotifier& notifier, uint256 hash, char label, std::optional<uint64_t> sequence = {})
257 : : {
258 : 42 : unsigned char data[sizeof(hash) + sizeof(label) + sizeof(uint64_t)];
259 [ + + ]: 1386 : for (unsigned int i = 0; i < sizeof(hash); ++i) {
260 : 1344 : data[sizeof(hash) - 1 - i] = hash.begin()[i];
261 : : }
262 : 42 : data[sizeof(hash)] = label;
263 [ + + ]: 42 : if (sequence) WriteLE64(data + sizeof(hash) + sizeof(label), *sequence);
264 [ + + ]: 56 : return notifier.SendZmqMessage(MSG_SEQUENCE, data, sequence ? sizeof(data) : sizeof(hash) + sizeof(label));
265 : : }
266 : :
267 : 12 : bool CZMQPublishSequenceNotifier::NotifyBlockConnect(const CBlockIndex *pindex)
268 : : {
269 : 12 : uint256 hash = pindex->GetBlockHash();
270 [ + - + - ]: 24 : LogDebug(BCLog::ZMQ, "Publish sequence block connect %s to %s\n", hash.GetHex(), this->address);
271 : 12 : return SendSequenceMsg(*this, hash, /* Block (C)onnect */ 'C');
272 : : }
273 : :
274 : 2 : bool CZMQPublishSequenceNotifier::NotifyBlockDisconnect(const CBlockIndex *pindex)
275 : : {
276 : 2 : uint256 hash = pindex->GetBlockHash();
277 [ + - + - ]: 4 : LogDebug(BCLog::ZMQ, "Publish sequence block disconnect %s to %s\n", hash.GetHex(), this->address);
278 : 2 : return SendSequenceMsg(*this, hash, /* Block (D)isconnect */ 'D');
279 : : }
280 : :
281 : 24 : bool CZMQPublishSequenceNotifier::NotifyTransactionAcceptance(const CTransaction &transaction, uint64_t mempool_sequence)
282 : : {
283 : 24 : uint256 hash = transaction.GetHash().ToUint256();
284 [ + - + - ]: 48 : LogDebug(BCLog::ZMQ, "Publish hashtx mempool acceptance %s to %s\n", hash.GetHex(), this->address);
285 : 24 : return SendSequenceMsg(*this, hash, /* Mempool (A)cceptance */ 'A', mempool_sequence);
286 : : }
287 : :
288 : 4 : bool CZMQPublishSequenceNotifier::NotifyTransactionRemoval(const CTransaction &transaction, uint64_t mempool_sequence)
289 : : {
290 : 4 : uint256 hash = transaction.GetHash().ToUint256();
291 [ + - + - ]: 8 : LogDebug(BCLog::ZMQ, "Publish hashtx mempool removal %s to %s\n", hash.GetHex(), this->address);
292 : 4 : return SendSequenceMsg(*this, hash, /* Mempool (R)emoval */ 'R', mempool_sequence);
293 : : }
|