| 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 | #include "bootstrap_node_selector.h" |
| 31 | |
| 32 | #include "crypto/crypto.h" |
| 33 | |
| 34 | namespace cryptonote |
| 35 | { |
| 36 | namespace bootstrap_node |
| 37 | { |
| 38 | |
| 39 | void selector_auto::node::handle_result(bool success) |
| 40 | { |
| 41 | if (!success) |
| 42 | { |
| 43 | fails = std::min(std::numeric_limits<size_t>::max() - 2, fails) + 2; |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | fails = std::max(std::numeric_limits<size_t>::min() + 2, fails) - 2; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void selector_auto::handle_result(const std::string &address, bool success) |
| 52 | { |
| 53 | auto &nodes_by_address = m_nodes.get<by_address>(); |
| 54 | const auto it = nodes_by_address.find(address); |
| 55 | if (it != nodes_by_address.end()) |
| 56 | { |
| 57 | nodes_by_address.modify(it, [success](node &entry) { |
| 58 | entry.handle_result(success); |
| 59 | }); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | boost::optional<node_info> selector_auto::next_node() |
| 64 | { |
| 65 | if (!has_at_least_one_good_node()) |
| 66 | { |
| 67 | append_new_nodes(); |
| 68 | } |
| 69 | |
| 70 | if (m_nodes.empty()) |
| 71 | { |
| 72 | return {}; |
| 73 | } |
| 74 | |
| 75 | auto node = m_nodes.get<by_fails>().begin(); |
| 76 | const size_t count = std::distance(node, m_nodes.get<by_fails>().upper_bound(node->fails)); |
| 77 | std::advance(node, crypto::rand_idx(count)); |
| 78 | |
| 79 | return {{node->address, {}}}; |
| 80 | } |
| 81 | |
| 82 | bool selector_auto::has_at_least_one_good_node() const |
| 83 | { |
| 84 | return !m_nodes.empty() && m_nodes.get<by_fails>().begin()->fails == 0; |
| 85 | } |
| 86 | |
| 87 | void selector_auto::append_new_nodes() |
| 88 | { |
| 89 | bool updated = false; |
| 90 | |
| 91 | for (const auto &node : m_get_nodes()) |
| 92 | { |
| 93 | const auto &address = node.first; |
| 94 | const auto &white = node.second; |
| 95 | const size_t initial_score = white ? 0 : 1; |
| 96 | updated |= m_nodes.get<by_address>().insert({address, initial_score}).second; |
| 97 | } |
| 98 | |
| 99 | if (updated) |
| 100 | { |
| 101 | truncate(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void selector_auto::truncate() |
| 106 | { |
| 107 | const size_t total = m_nodes.size(); |
| 108 | if (total > m_max_nodes) |
| 109 | { |
| 110 | auto &nodes_by_fails = m_nodes.get<by_fails>(); |
| 111 | auto from = nodes_by_fails.rbegin(); |
| 112 | std::advance(from, total - m_max_nodes); |
| 113 | nodes_by_fails.erase(from.base(), nodes_by_fails.end()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | } |