Debian Codegraph @

src/rpc/zmq_pub.h

raw symbols SCIP
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"
43 #include "cryptonote_basic/difficulty.h"
44
45 namespace cryptonote { namespace listener
46 {
47 /*! \brief Sends ZMQ PUB messages on cryptonote events
48
49 Clients must ensure that all transaction(s) are notified before any blocks
50 they are contained in, and must ensure that each block is notified in chain
51 order. An external lock **must** be held by clients during the entire
52 txpool check and notification sequence and (a possibly second) lock is held
53 during the entire block check and notification sequence. Otherwise, events
54 could be sent in a different order than processed. */
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
61 net::zmq::socket relay_;
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_; //!< Synchronizes counts in `*_subs_` arrays.
67
68 public:
69 //! \return Name of ZMQ_PAIR endpoint for pub notifications
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
82 //! Process a client subscription request (from XPUB sockets). Thread-safe.
83 bool sub_request(const boost::string_ref message);
84
85 /*! Forward ZMQ messages sent to `relay` via `send_chain_main` or
86 `send_txpool_add` to `pub`. Used by `ZmqServer`. */
87 bool relay_to_pub(void* relay, void* pub);
88
89 /*! Send a `ZMQ_PUB` notification for a change to the main chain.
90 Thread-safe.
91 \return Number of ZMQ messages sent to relay. */
92 std::size_t send_chain_main(std::uint64_t height, epee::span<const cryptonote::block> blocks);
93
94 /*! Send a `ZMQ_PUB` notification for a new miner data.
95 Thread-safe.
96 \return Number of ZMQ messages sent to relay. */
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
99 /*! Send a `ZMQ_PUB` notification for new tx(es) being added to the local
100 pool. Thread-safe.
101 \return Number of ZMQ messages sent to relay. */
102 std::size_t send_txpool_add(std::vector<cryptonote::txpool_event> txes);
103
104 //! Callable for `send_chain_main` with weak ownership to `zmq_pub` object.
105 struct chain_main
106 {
107 std::weak_ptr<zmq_pub> self_;
108 void operator()(std::uint64_t height, epee::span<const cryptonote::block> blocks) const;
109 };
110
111 //! Callable for `send_miner_data` with weak ownership to `zmq_pub` object.
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
118 //! Callable for `send_txpool_add` with weak ownership to `zmq_pub` object.
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 }}