| 1 | // Copyright (c) 2019-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include <boost/asio/io_context.hpp> |
| 32 | #include <boost/uuid/uuid.hpp> |
| 33 | #include <memory> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "byte_slice.h" |
| 37 | #include "cryptonote_basic/blobdatatype.h" |
| 38 | #include "cryptonote_protocol/enums.h" |
| 39 | #include "cryptonote_protocol/fwd.h" |
| 40 | #include "net/enums.h" |
| 41 | #include "span.h" |
| 42 | |
| 43 | namespace epee |
| 44 | { |
| 45 | namespace levin |
| 46 | { |
| 47 | template<typename> class async_protocol_handler_config; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | namespace nodetool |
| 52 | { |
| 53 | template<typename> struct p2p_connection_context_t; |
| 54 | } |
| 55 | |
| 56 | namespace cryptonote |
| 57 | { |
| 58 | namespace levin |
| 59 | { |
| 60 | namespace detail |
| 61 | { |
| 62 | using p2p_context = nodetool::p2p_connection_context_t<cryptonote::cryptonote_connection_context>; |
| 63 | struct zone; //!< Internal data needed for zone notifications |
| 64 | } // detail |
| 65 | |
| 66 | using connections = epee::levin::async_protocol_handler_config<detail::p2p_context>; |
| 67 | |
| 68 | //! Provides tx notification privacy |
| 69 | class notify |
| 70 | { |
| 71 | std::shared_ptr<detail::zone> zone_; |
| 72 | i_core_events* core_; |
| 73 | |
| 74 | public: |
| 75 | struct status |
| 76 | { |
| 77 | bool has_noise; |
| 78 | bool connections_filled; //!< True when has zone has `CRYPTONOTE_NOISE_CHANNELS` outgoing noise channels |
| 79 | bool has_outgoing; //!< True when zone has outgoing connections |
| 80 | }; |
| 81 | |
| 82 | //! Construct an instance that cannot notify. |
| 83 | notify() noexcept |
| 84 | : zone_(nullptr) |
| 85 | , core_(nullptr) |
| 86 | {} |
| 87 | |
| 88 | //! Construct an instance with available notification `zones`. |
| 89 | explicit notify(boost::asio::io_context& service, std::shared_ptr<connections> p2p, epee::byte_slice noise, epee::net_utils::zone zone, bool pad_txs, i_core_events& core); |
| 90 | |
| 91 | notify(const notify&) = delete; |
| 92 | notify(notify&&) = default; |
| 93 | |
| 94 | ~notify() noexcept; |
| 95 | |
| 96 | notify& operator=(const notify&) = delete; |
| 97 | notify& operator=(notify&&) = default; |
| 98 | |
| 99 | //! \return Status information for zone selection. |
| 100 | status get_status() const noexcept; |
| 101 | |
| 102 | //! Probe for new outbound connection - skips if not needed. |
| 103 | void new_out_connection(); |
| 104 | |
| 105 | void on_handshake_complete(const boost::uuids::uuid &id, bool is_income); |
| 106 | void on_connection_close(const boost::uuids::uuid &id); |
| 107 | |
| 108 | //! Run the logic for the next epoch immediately. Only use in testing. |
| 109 | void run_epoch(); |
| 110 | |
| 111 | //! Run the logic for the next stem timeout imemdiately. Only use in testing. |
| 112 | void run_stems(); |
| 113 | |
| 114 | //! Run the logic for flushing all Dandelion++ fluff queued txs. Only use in testing. |
| 115 | void run_fluff(); |
| 116 | |
| 117 | /*! Send txs using `cryptonote_protocol_defs.h` payload format wrapped in a |
| 118 | levin header. The message will be sent in a "discreet" manner if |
| 119 | enabled - if `!noise.empty()` then the `command`/`payload` will be |
| 120 | queued to send at the next available noise interval. Otherwise, a |
| 121 | Dandelion++ fluff algorithm will be used. |
| 122 | |
| 123 | \note Eventually Dandelion++ stem sending will be used here when |
| 124 | enabled. |
| 125 | |
| 126 | \param txs The transactions that need to be serialized and relayed. |
| 127 | \param source The source of the notification. `is_nil()` indicates this |
| 128 | node is the source. Dandelion++ will use this to map a source to a |
| 129 | particular stem. |
| 130 | |
| 131 | \return True iff the notification is queued for sending. */ |
| 132 | bool send_txs(std::vector<blobdata> txs, const boost::uuids::uuid& source, relay_method tx_relay); |
| 133 | }; |
| 134 | } // levin |
| 135 | } // net |