| 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 | #include "dandelionpp.h" |
| 30 | |
| 31 | #include <boost/container/small_vector.hpp> |
| 32 | #include <boost/uuid/nil_generator.hpp> |
| 33 | #include <chrono> |
| 34 | |
| 35 | #include "common/expect.h" |
| 36 | #include "cryptonote_config.h" |
| 37 | #include "crypto/crypto.h" |
| 38 | |
| 39 | namespace net |
| 40 | { |
| 41 | namespace dandelionpp |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | constexpr const std::size_t expected_max_channels = CRYPTONOTE_NOISE_CHANNELS; |
| 46 | |
| 47 | // could be in util somewhere |
| 48 | struct key_less |
| 49 | { |
| 50 | template<typename K, typename V> |
| 51 | bool operator()(const std::pair<K, V>& left, const K& right) const |
| 52 | { |
| 53 | return left.first < right; |
| 54 | } |
| 55 | |
| 56 | template<typename K, typename V> |
| 57 | bool operator()(const K& left, const std::pair<K, V>& right) const |
| 58 | { |
| 59 | return left < right.first; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | std::size_t select_stem(epee::span<const std::size_t> usage, epee::span<const boost::uuids::uuid> out_map) |
| 64 | { |
| 65 | assert(usage.size() < std::numeric_limits<std::size_t>::max()); // prevented in constructor |
| 66 | if (usage.size() < out_map.size()) |
| 67 | return std::numeric_limits<std::size_t>::max(); |
| 68 | |
| 69 | // small_vector uses stack space if `expected_max_channels < capacity()` |
| 70 | std::size_t lowest = std::numeric_limits<std::size_t>::max(); |
| 71 | boost::container::small_vector<std::size_t, expected_max_channels> choices; |
| 72 | static_assert(sizeof(choices) < 256, "choices is too large based on current configuration"); |
| 73 | |
| 74 | for (const boost::uuids::uuid& out : out_map) |
| 75 | { |
| 76 | if (!out.is_nil()) |
| 77 | { |
| 78 | const std::size_t location = std::addressof(out) - out_map.begin(); |
| 79 | if (usage[location] < lowest) |
| 80 | { |
| 81 | lowest = usage[location]; |
| 82 | choices = {location}; |
| 83 | } |
| 84 | else if (usage[location] == lowest) |
| 85 | choices.push_back(location); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | switch (choices.size()) |
| 90 | { |
| 91 | case 0: |
| 92 | return std::numeric_limits<std::size_t>::max(); |
| 93 | case 1: |
| 94 | return choices[0]; |
| 95 | default: |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | return choices[crypto::rand_idx(choices.size())]; |
| 100 | } |
| 101 | } // anonymous |
| 102 | |
| 103 | connection_map::connection_map(std::vector<boost::uuids::uuid> out_connections, const std::size_t stems) |
| 104 | : out_mapping_(std::move(out_connections)), |
| 105 | in_mapping_(), |
| 106 | usage_count_() |
| 107 | { |
| 108 | // max value is used by `select_stem` as error case |
| 109 | if (stems == std::numeric_limits<std::size_t>::max()) |
| 110 | MONERO_THROW(common_error::kInvalidArgument, "stems value cannot be max size_t"); |
| 111 | |
| 112 | usage_count_.resize(stems); |
| 113 | if (stems < out_mapping_.size()) |
| 114 | { |
| 115 | for (unsigned i = 0; i < stems; ++i) |
| 116 | std::swap(out_mapping_[i], out_mapping_.at(i + crypto::rand_idx(out_mapping_.size() - i))); |
| 117 | |
| 118 | out_mapping_.resize(stems); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | std::shuffle(out_mapping_.begin(), out_mapping_.end(), crypto::random_device{}); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | connection_map::~connection_map() noexcept |
| 127 | {} |
| 128 | |
| 129 | connection_map connection_map::clone() const |
| 130 | { |
| 131 | return {*this}; |
| 132 | } |
| 133 | |
| 134 | bool connection_map::update(std::vector<boost::uuids::uuid> current) |
| 135 | { |
| 136 | std::sort(current.begin(), current.end()); |
| 137 | |
| 138 | bool replace = false; |
| 139 | for (auto& existing_out : out_mapping_) |
| 140 | { |
| 141 | const auto elem = std::lower_bound(current.begin(), current.end(), existing_out); |
| 142 | if (elem == current.end() || *elem != existing_out) |
| 143 | { |
| 144 | existing_out = boost::uuids::nil_uuid(); |
| 145 | replace = true; |
| 146 | } |
| 147 | else // already using connection, remove it from candidate list |
| 148 | current.erase(elem); |
| 149 | } |
| 150 | |
| 151 | if (!replace && out_mapping_.size() == usage_count_.size()) |
| 152 | return false; |
| 153 | |
| 154 | const std::size_t existing_outs = out_mapping_.size(); |
| 155 | for (std::size_t i = 0; i < usage_count_.size() && !current.empty(); ++i) |
| 156 | { |
| 157 | const bool increase_stems = out_mapping_.size() <= i; |
| 158 | if (increase_stems || out_mapping_[i].is_nil()) |
| 159 | { |
| 160 | std::swap(current.back(), current.at(crypto::rand_idx(current.size()))); |
| 161 | if (increase_stems) |
| 162 | out_mapping_.push_back(current.back()); |
| 163 | else |
| 164 | out_mapping_[i] = current.back(); |
| 165 | current.pop_back(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return replace || existing_outs < out_mapping_.size(); |
| 170 | } |
| 171 | |
| 172 | std::size_t connection_map::size() const noexcept |
| 173 | { |
| 174 | std::size_t count = 0; |
| 175 | for (const boost::uuids::uuid& connection : out_mapping_) |
| 176 | { |
| 177 | if (!connection.is_nil()) |
| 178 | ++count; |
| 179 | } |
| 180 | return count; |
| 181 | } |
| 182 | |
| 183 | boost::uuids::uuid connection_map::get_stem(const boost::uuids::uuid& source) |
| 184 | { |
| 185 | auto elem = std::lower_bound(in_mapping_.begin(), in_mapping_.end(), source, key_less{}); |
| 186 | if (elem == in_mapping_.end() || elem->first != source) |
| 187 | { |
| 188 | const std::size_t index = select_stem(epee::to_span(usage_count_), epee::to_span(out_mapping_)); |
| 189 | if (out_mapping_.size() < index) |
| 190 | return boost::uuids::nil_uuid(); |
| 191 | |
| 192 | elem = in_mapping_.emplace(elem, source, index); |
| 193 | usage_count_[index]++; |
| 194 | } |
| 195 | else if (out_mapping_.at(elem->second).is_nil()) // stem connection disconnected after mapping |
| 196 | { |
| 197 | usage_count_.at(elem->second)--; |
| 198 | const std::size_t index = select_stem(epee::to_span(usage_count_), epee::to_span(out_mapping_)); |
| 199 | if (out_mapping_.size() < index) |
| 200 | { |
| 201 | in_mapping_.erase(elem); |
| 202 | return boost::uuids::nil_uuid(); |
| 203 | } |
| 204 | |
| 205 | elem->second = index; |
| 206 | usage_count_[index]++; |
| 207 | } |
| 208 | |
| 209 | return out_mapping_[elem->second]; |
| 210 | } |
| 211 | } // dandelionpp |
| 212 | } // net |