|
17 | 17 | #include <cstdarg>
|
18 | 18 | #include <cstddef>
|
19 | 19 | #include <map>
|
| 20 | +#include <optional> |
20 | 21 | #include <string>
|
21 | 22 | #include <utility>
|
22 | 23 |
|
@@ -227,50 +228,43 @@ bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &tr
|
227 | 228 | return SendZmqMessage(MSG_RAWTX, &(*ss.begin()), ss.size());
|
228 | 229 | }
|
229 | 230 |
|
| 231 | +// Helper function to send a 'sequence' topic message with the following structure: |
| 232 | +// <32-byte hash> | <1-byte label> | <8-byte LE sequence> (optional) |
| 233 | +static bool SendSequenceMsg(CZMQAbstractPublishNotifier& notifier, uint256 hash, char label, std::optional<uint64_t> sequence = {}) |
| 234 | +{ |
| 235 | + unsigned char data[sizeof(hash) + sizeof(label) + sizeof(uint64_t)]; |
| 236 | + for (unsigned int i = 0; i < sizeof(hash); ++i) { |
| 237 | + data[sizeof(hash) - 1 - i] = hash.begin()[i]; |
| 238 | + } |
| 239 | + data[sizeof(hash)] = label; |
| 240 | + if (sequence) WriteLE64(data + sizeof(hash) + sizeof(label), *sequence); |
| 241 | + return notifier.SendZmqMessage(MSG_SEQUENCE, data, sequence ? sizeof(data) : sizeof(hash) + sizeof(label)); |
| 242 | +} |
230 | 243 |
|
231 |
| -// TODO: Dedup this code to take label char, log string |
232 | 244 | bool CZMQPublishSequenceNotifier::NotifyBlockConnect(const CBlockIndex *pindex)
|
233 | 245 | {
|
234 | 246 | uint256 hash = pindex->GetBlockHash();
|
235 | 247 | LogPrint(BCLog::ZMQ, "zmq: Publish sequence block connect %s to %s\n", hash.GetHex(), this->address);
|
236 |
| - char data[sizeof(uint256)+1]; |
237 |
| - for (unsigned int i = 0; i < sizeof(uint256); i++) |
238 |
| - data[sizeof(uint256) - 1 - i] = hash.begin()[i]; |
239 |
| - data[sizeof(data) - 1] = 'C'; // Block (C)onnect |
240 |
| - return SendZmqMessage(MSG_SEQUENCE, data, sizeof(data)); |
| 248 | + return SendSequenceMsg(*this, hash, /* Block (C)onnect */ 'C'); |
241 | 249 | }
|
242 | 250 |
|
243 | 251 | bool CZMQPublishSequenceNotifier::NotifyBlockDisconnect(const CBlockIndex *pindex)
|
244 | 252 | {
|
245 | 253 | uint256 hash = pindex->GetBlockHash();
|
246 | 254 | LogPrint(BCLog::ZMQ, "zmq: Publish sequence block disconnect %s to %s\n", hash.GetHex(), this->address);
|
247 |
| - char data[sizeof(uint256)+1]; |
248 |
| - for (unsigned int i = 0; i < sizeof(uint256); i++) |
249 |
| - data[sizeof(uint256) - 1 - i] = hash.begin()[i]; |
250 |
| - data[sizeof(data) - 1] = 'D'; // Block (D)isconnect |
251 |
| - return SendZmqMessage(MSG_SEQUENCE, data, sizeof(data)); |
| 255 | + return SendSequenceMsg(*this, hash, /* Block (D)isconnect */ 'D'); |
252 | 256 | }
|
253 | 257 |
|
254 | 258 | bool CZMQPublishSequenceNotifier::NotifyTransactionAcceptance(const CTransaction &transaction, uint64_t mempool_sequence)
|
255 | 259 | {
|
256 | 260 | uint256 hash = transaction.GetHash();
|
257 | 261 | LogPrint(BCLog::ZMQ, "zmq: Publish hashtx mempool acceptance %s to %s\n", hash.GetHex(), this->address);
|
258 |
| - unsigned char data[sizeof(uint256)+sizeof(mempool_sequence)+1]; |
259 |
| - for (unsigned int i = 0; i < sizeof(uint256); i++) |
260 |
| - data[sizeof(uint256) - 1 - i] = hash.begin()[i]; |
261 |
| - data[sizeof(uint256)] = 'A'; // Mempool (A)cceptance |
262 |
| - WriteLE64(data+sizeof(uint256)+1, mempool_sequence); |
263 |
| - return SendZmqMessage(MSG_SEQUENCE, data, sizeof(data)); |
| 262 | + return SendSequenceMsg(*this, hash, /* Mempool (A)cceptance */ 'A', mempool_sequence); |
264 | 263 | }
|
265 | 264 |
|
266 | 265 | bool CZMQPublishSequenceNotifier::NotifyTransactionRemoval(const CTransaction &transaction, uint64_t mempool_sequence)
|
267 | 266 | {
|
268 | 267 | uint256 hash = transaction.GetHash();
|
269 | 268 | LogPrint(BCLog::ZMQ, "zmq: Publish hashtx mempool removal %s to %s\n", hash.GetHex(), this->address);
|
270 |
| - unsigned char data[sizeof(uint256)+sizeof(mempool_sequence)+1]; |
271 |
| - for (unsigned int i = 0; i < sizeof(uint256); i++) |
272 |
| - data[sizeof(uint256) - 1 - i] = hash.begin()[i]; |
273 |
| - data[sizeof(uint256)] = 'R'; // Mempool (R)emoval |
274 |
| - WriteLE64(data+sizeof(uint256)+1, mempool_sequence); |
275 |
| - return SendZmqMessage(MSG_SEQUENCE, data, sizeof(data)); |
| 269 | + return SendSequenceMsg(*this, hash, /* Mempool (R)emoval */ 'R', mempool_sequence); |
276 | 270 | }
|
0 commit comments