Bitcoin Core  27.99.0
P2P Digital Currency
peer_eviction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <bench/bench.h>
6 #include <net.h>
7 #include <netaddress.h>
8 #include <random.h>
9 #include <test/util/net.h>
10 #include <test/util/setup_common.h>
11 
12 #include <algorithm>
13 #include <functional>
14 #include <vector>
15 
17  benchmark::Bench& bench,
18  int num_candidates,
19  std::function<void(NodeEvictionCandidate&)> candidate_setup_fn)
20 {
21  using Candidates = std::vector<NodeEvictionCandidate>;
22  FastRandomContext random_context{true};
23 
24  Candidates candidates{GetRandomNodeEvictionCandidates(num_candidates, random_context)};
25  for (auto& c : candidates) {
26  candidate_setup_fn(c);
27  }
28 
29 
30  bench.run([&] {
31  // creating a copy has an overhead of about 3%, so it does not influence the benchmark results much.
32  auto copy = candidates;
34  });
35 }
36 
37 /* Benchmarks */
38 
40 {
42  bench,
43  /*num_candidates=*/250,
44  [](NodeEvictionCandidate& c) {
45  c.m_connected = std::chrono::seconds{c.id};
46  c.m_network = NET_IPV4;
47  });
48 }
49 
51 {
53  bench,
54  /*num_candidates=*/250,
55  [](NodeEvictionCandidate& c) {
56  c.m_connected = std::chrono::seconds{c.id};
57  c.m_is_local = false;
58  if (c.id >= 130 && c.id < 240) { // 110 Tor
59  c.m_network = NET_ONION;
60  } else {
61  c.m_network = NET_IPV4;
62  }
63  });
64 }
65 
67 {
69  bench,
70  /*num_candidates=*/250,
71  [](NodeEvictionCandidate& c) {
72  c.m_connected = std::chrono::seconds{c.id};
73  c.m_is_local = false;
74  if (c.id >= 90 && c.id < 160) { // 70 Tor
75  c.m_network = NET_ONION;
76  } else if (c.id >= 170 && c.id < 250) { // 80 I2P
77  c.m_network = NET_I2P;
78  } else {
79  c.m_network = NET_IPV4;
80  }
81  });
82 }
83 
85 {
87  bench,
88  /*num_candidates=*/50,
89  [](NodeEvictionCandidate& c) {
90  c.m_connected = std::chrono::seconds{c.id};
91  c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost
92  if (c.id >= 30 && c.id < 47) { // 17 I2P
93  c.m_network = NET_I2P;
94  } else if (c.id >= 24 && c.id < 28) { // 4 Tor
95  c.m_network = NET_ONION;
96  } else {
97  c.m_network = NET_IPV4;
98  }
99  });
100 }
101 
103 {
105  bench,
106  /*num_candidates=*/100,
107  [](NodeEvictionCandidate& c) {
108  c.m_connected = std::chrono::seconds{c.id};
109  c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost
110  if (c.id >= 70 && c.id < 80) { // 10 I2P
111  c.m_network = NET_I2P;
112  } else if (c.id >= 80 && c.id < 96) { // 16 Tor
113  c.m_network = NET_ONION;
114  } else {
115  c.m_network = NET_IPV4;
116  }
117  });
118 }
119 
121 {
123  bench,
124  /*num_candidates=*/250,
125  [](NodeEvictionCandidate& c) {
126  c.m_connected = std::chrono::seconds{c.id};
127  c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost
128  if (c.id >= 170 && c.id < 180) { // 10 I2P
129  c.m_network = NET_I2P;
130  } else if (c.id >= 190 && c.id < 240) { // 50 Tor
131  c.m_network = NET_ONION;
132  } else {
133  c.m_network = NET_IPV4;
134  }
135  });
136 }
137 
138 // Candidate numbers used for the benchmarks:
139 // - 50 candidates simulates a possible use of -maxconnections
140 // - 100 candidates approximates an average node with default settings
141 // - 250 candidates is the number of peers reported by operators of busy nodes
142 
143 // No disadvantaged networks, with 250 eviction candidates.
145 
146 // 1 disadvantaged network (Tor) with 250 eviction candidates.
148 
149 // 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates.
151 
152 // 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction candidates.
if(!SetupNetworking())
Fast randomness source.
Definition: random.h:145
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
Definition: eviction.cpp:105
@ HIGH
Definition: bench.h:47
@ NET_I2P
I2P.
Definition: netaddress.h:46
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:43
@ NET_IPV4
IPv4.
Definition: netaddress.h:37
static void EvictionProtection2Networks250Candidates(benchmark::Bench &bench)
static void EvictionProtection3Networks100Candidates(benchmark::Bench &bench)
static void EvictionProtectionCommon(benchmark::Bench &bench, int num_candidates, std::function< void(NodeEvictionCandidate &)> candidate_setup_fn)
static void EvictionProtection1Networks250Candidates(benchmark::Bench &bench)
static void EvictionProtection3Networks250Candidates(benchmark::Bench &bench)
static void EvictionProtection0Networks250Candidates(benchmark::Bench &bench)
static void EvictionProtection3Networks050Candidates(benchmark::Bench &bench)
BENCHMARK(EvictionProtection0Networks250Candidates, benchmark::PriorityLevel::HIGH)
std::chrono::seconds m_connected
Definition: eviction.h:20
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:115