Monero
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 
40 #include "cryptonote_basic/fwd.h"
41 #include "net/zmq.h"
42 #include "span.h"
44 
45 namespace cryptonote { namespace listener
46 {
55 class zmq_pub
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;
80  zmq_pub& operator=(zmq_pub&&) = delete;
81 
83  bool sub_request(const boost::string_ref message);
84 
87  bool relay_to_pub(void* relay, void* pub);
88 
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 
105  struct chain_main
106  {
107  std::weak_ptr<zmq_pub> self_;
109  };
110 
112  struct miner_data
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 
119  struct txpool_add
120  {
121  std::weak_ptr<zmq_pub> self_;
122  void operator()(std::vector<cryptonote::txpool_event> txes) const;
123  };
124  };
125 }}
Callable for send_miner_data with weak ownership to zmq_pub object.
Definition: zmq_pub.h:112
std::array< std::size_t, 2 > chain_subs_
Definition: zmq_pub.h:63
zmq_pub(void *context)
Definition: zmq_pub.cpp:360
bool sub_request(const boost::string_ref message)
Process a client subscription request (from XPUB sockets). Thread-safe.
Definition: zmq_pub.cpp:384
static constexpr const char * relay_endpoint() noexcept
Definition: zmq_pub.h:70
std::array< std::size_t, 2 > txpool_subs_
Definition: zmq_pub.h:65
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:108
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
Non-owning sequence of data. Does not deep copy.
Definition: span.h:54
Definition: blocks.cpp:12
unsigned char uint8_t
Definition: stdint.h:124
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:114
Sends ZMQ PUB messages on cryptonote events.
Definition: zmq_pub.h:55
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:44
bool relay_to_pub(void *relay, void *pub)
Definition: zmq_pub.cpp:422
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
boost::mutex sync_
Synchronizes counts in *_subs_ arrays.
Definition: zmq_pub.h:66
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_txpool_add with weak ownership to zmq_pub object.
Definition: zmq_pub.h:119
net::zmq::socket relay_
Definition: zmq_pub.h:61
unsigned __int64 uint64_t
Definition: stdint.h:136
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:105
diff
Definition: gen_wide_data.py:42
std::size_t send_chain_main(std::uint64_t height, epee::span< const cryptonote::block > blocks)
Definition: zmq_pub.cpp:454
Callable for send_chain_main with weak ownership to zmq_pub object.
Definition: zmq_pub.h:105
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:41
std::size_t send_txpool_add(std::vector< cryptonote::txpool_event > txes)
Definition: zmq_pub.cpp:502
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:121
void operator()(std::vector< cryptonote::txpool_event > txes) const
Definition: zmq_pub.cpp:541
std::deque< std::vector< txpool_event > > txes_
Definition: zmq_pub.h:62
POD_CLASS hash
Definition: hash.h:49
major_version
Definition: versiongenerate.py:78
std::array< std::size_t, 1 > miner_subs_
Definition: zmq_pub.h:64
tuple message
Definition: gtest_output_test.py:331
~zmq_pub()
Definition: zmq_pub.cpp:381
zmq_pub & operator=(const zmq_pub &)=delete