Monero
Loading...
Searching...
No Matches
zmq_pub.h
Go to the documentation of this file.
1// Copyright (c) 2020-2022, The Monero Project
2
3//
4// All rights reserved.
5 //
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#pragma once
31
32#include <array>
33#include <boost/thread/mutex.hpp>
34#include <boost/utility/string_ref.hpp>
35#include <cstdint>
36#include <deque>
37#include <memory>
38#include <vector>
39
41#include "net/zmq.h"
42#include "span.h"
44
45namespace cryptonote { namespace listener
46{
56{
57 /* Each socket has its own internal queue. So we can only use one socket, else
58 the messages being published are not guaranteed to be in the same order
59 pushed. */
60
62 std::deque<std::vector<txpool_event>> txes_;
63 std::array<std::size_t, 2> chain_subs_;
64 std::array<std::size_t, 1> miner_subs_;
65 std::array<std::size_t, 2> txpool_subs_;
66 boost::mutex sync_;
67
68 public:
70 static constexpr const char* relay_endpoint() noexcept { return "inproc://pub_relay"; }
71
72 explicit zmq_pub(void* context);
73
74 zmq_pub(const zmq_pub&) = delete;
75 zmq_pub(zmq_pub&&) = delete;
76
77 ~zmq_pub();
78
79 zmq_pub& operator=(const zmq_pub&) = delete;
81
83 bool sub_request(const boost::string_ref message);
84
87 bool relay_to_pub(void* relay, void* pub);
88
92 std::size_t send_chain_main(std::uint64_t height, epee::span<const cryptonote::block> blocks);
93
97 std::size_t send_miner_data(uint8_t major_version, uint64_t height, const crypto::hash& prev_id, const crypto::hash& seed_hash, difficulty_type diff, uint64_t median_weight, uint64_t already_generated_coins, const std::vector<tx_block_template_backlog_entry>& tx_backlog);
98
102 std::size_t send_txpool_add(std::vector<cryptonote::txpool_event> txes);
103
106 {
107 std::weak_ptr<zmq_pub> self_;
108 void operator()(std::uint64_t height, epee::span<const cryptonote::block> blocks) const;
109 };
110
113 {
114 std::weak_ptr<zmq_pub> self_;
115 void operator()(uint8_t major_version, uint64_t height, const crypto::hash& prev_id, const crypto::hash& seed_hash, difficulty_type diff, uint64_t median_weight, uint64_t already_generated_coins, const std::vector<tx_block_template_backlog_entry>& tx_backlog) const;
116 };
117
120 {
121 std::weak_ptr<zmq_pub> self_;
122 void operator()(std::vector<cryptonote::txpool_event> txes) const;
123 };
124 };
125}}
Sends ZMQ PUB messages on cryptonote events.
Definition: zmq_pub.h:56
std::array< std::size_t, 1 > miner_subs_
Definition: zmq_pub.h:64
std::deque< std::vector< txpool_event > > txes_
Definition: zmq_pub.h:62
zmq_pub(const zmq_pub &)=delete
boost::mutex sync_
Synchronizes counts in *_subs_ arrays.
Definition: zmq_pub.h:66
zmq_pub & operator=(const zmq_pub &)=delete
zmq_pub(zmq_pub &&)=delete
std::array< std::size_t, 2 > chain_subs_
Definition: zmq_pub.h:63
~zmq_pub()
Definition: zmq_pub.cpp:381
static constexpr const char * relay_endpoint() noexcept
Definition: zmq_pub.h:70
std::size_t send_miner_data(uint8_t major_version, uint64_t height, const crypto::hash &prev_id, const crypto::hash &seed_hash, difficulty_type diff, uint64_t median_weight, uint64_t already_generated_coins, const std::vector< tx_block_template_backlog_entry > &tx_backlog)
Definition: zmq_pub.cpp:483
std::array< std::size_t, 2 > txpool_subs_
Definition: zmq_pub.h:65
zmq_pub & operator=(zmq_pub &&)=delete
bool relay_to_pub(void *relay, void *pub)
Definition: zmq_pub.cpp:422
std::size_t send_txpool_add(std::vector< cryptonote::txpool_event > txes)
Definition: zmq_pub.cpp:502
net::zmq::socket relay_
Definition: zmq_pub.h:61
bool sub_request(const boost::string_ref message)
Process a client subscription request (from XPUB sockets). Thread-safe.
Definition: zmq_pub.cpp:384
std::size_t send_chain_main(std::uint64_t height, epee::span< const cryptonote::block > blocks)
Definition: zmq_pub.cpp:454
Non-owning sequence of data. Does not deep copy.
Definition: span.h:55
Definition: blocks.cpp:13
POD_CLASS hash
Definition: hash.h:48
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:41
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:108
unsigned char uint8_t
Definition: stdint.h:124
Callable for send_chain_main with weak ownership to zmq_pub object.
Definition: zmq_pub.h:106
void operator()(std::uint64_t height, epee::span< const cryptonote::block > blocks) const
Definition: zmq_pub.cpp:523
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:107
Callable for send_miner_data with weak ownership to zmq_pub object.
Definition: zmq_pub.h:113
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:114
void operator()(uint8_t major_version, uint64_t height, const crypto::hash &prev_id, const crypto::hash &seed_hash, difficulty_type diff, uint64_t median_weight, uint64_t already_generated_coins, const std::vector< tx_block_template_backlog_entry > &tx_backlog) const
Definition: zmq_pub.cpp:532
Callable for send_txpool_add with weak ownership to zmq_pub object.
Definition: zmq_pub.h:120
void operator()(std::vector< cryptonote::txpool_event > txes) const
Definition: zmq_pub.cpp:541
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:121