Monero
bootstrap_node_selector.h
Go to the documentation of this file.
1 // Copyright (c) 2020, 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 <functional>
32 #include <limits>
33 #include <map>
34 #include <string>
35 #include <utility>
36 
37 #include <boost/multi_index_container.hpp>
38 #include <boost/multi_index/member.hpp>
39 #include <boost/multi_index/ordered_index.hpp>
40 #include <boost/optional/optional.hpp>
41 
42 #include "net/http_client.h"
43 
44 namespace cryptonote
45 {
46 namespace bootstrap_node
47 {
48 
49  struct node_info
50  {
51  std::string address;
52  boost::optional<epee::net_utils::http::login> credentials;
53  };
54 
55  struct selector
56  {
57  virtual ~selector() = default;
58 
59  virtual void handle_result(const std::string &address, bool success) = 0;
60  virtual boost::optional<node_info> next_node() = 0;
61  };
62 
63  class selector_auto : public selector
64  {
65  public:
66  selector_auto(std::function<std::map<std::string, bool>()> get_nodes, size_t max_nodes = 1000)
67  : m_get_nodes(std::move(get_nodes))
68  , m_max_nodes(max_nodes)
69  {}
70 
71  void handle_result(const std::string &address, bool success) final;
72  boost::optional<node_info> next_node() final;
73 
74  private:
76  void append_new_nodes();
77  void truncate();
78 
79  private:
80  struct node
81  {
82  std::string address;
83  size_t fails;
84 
85  void handle_result(bool success);
86  };
87 
88  struct by_address {};
89  struct by_fails {};
90 
91  typedef boost::multi_index_container<
92  node,
93  boost::multi_index::indexed_by<
94  boost::multi_index::ordered_unique<boost::multi_index::tag<by_address>, boost::multi_index::member<node, std::string, &node::address>>,
95  boost::multi_index::ordered_non_unique<boost::multi_index::tag<by_fails>, boost::multi_index::member<node, size_t, &node::fails>>
96  >
98 
99  const std::function<std::map<std::string, bool>()> m_get_nodes;
100  const size_t m_max_nodes;
102  };
103 
104 }
105 }
Definition: bootstrap_node_selector.h:64
void append_new_nodes()
Definition: bootstrap_node_selector.cpp:86
selector_auto(std::function< std::map< std::string, bool >()> get_nodes, size_t max_nodes=1000)
Definition: bootstrap_node_selector.h:66
boost::optional< node_info > next_node() final
Definition: bootstrap_node_selector.cpp:62
nodes_list m_nodes
Definition: bootstrap_node_selector.h:101
bool has_at_least_one_good_node() const
Definition: bootstrap_node_selector.cpp:81
void truncate()
Definition: bootstrap_node_selector.cpp:104
const std::function< std::map< std::string, bool >)> m_get_nodes
Definition: bootstrap_node_selector.h:99
boost::multi_index_container< node, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< by_address >, boost::multi_index::member< node, std::string, &node::address > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< by_fails >, boost::multi_index::member< node, size_t, &node::fails > > > > nodes_list
Definition: bootstrap_node_selector.h:97
void handle_result(const std::string &address, bool success) final
Definition: bootstrap_node_selector.cpp:50
const size_t m_max_nodes
Definition: bootstrap_node_selector.h:100
expect< void > success() noexcept
Definition: expect.h:397
const
Definition: build_protob.py:9
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
Definition: blockchain_ancestry.cpp:72
Definition: bootstrap_node_selector.h:50
boost::optional< epee::net_utils::http::login > credentials
Definition: bootstrap_node_selector.h:52
std::string address
Definition: bootstrap_node_selector.h:51
Definition: bootstrap_node_selector.h:88
Definition: bootstrap_node_selector.h:89
Definition: bootstrap_node_selector.h:81
size_t fails
Definition: bootstrap_node_selector.h:83
std::string address
Definition: bootstrap_node_selector.h:82
Definition: bootstrap_node_selector.h:56
virtual void handle_result(const std::string &address, bool success)=0
virtual boost::optional< node_info > next_node()=0