Bitcoin Core  22.99.0
P2P Digital Currency
peer_eviction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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  250 /* num_candidates */,
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  250 /* num_candidates */,
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  250 /* num_candidates */,
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  50 /* num_candidates */,
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  100 /* num_candidates */,
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  250 /* num_candidates */,
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.
NodeEvictionCandidate::m_network
Network m_network
Definition: net.h:1292
BENCHMARK
BENCHMARK(EvictionProtection0Networks250Candidates)
if
if(na.IsAddrV1Compatible())
Definition: deserialize.cpp:227
NET_IPV4
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
EvictionProtectionCommon
static void EvictionProtectionCommon(benchmark::Bench &bench, int num_candidates, std::function< void(NodeEvictionCandidate &)> candidate_setup_fn)
Definition: peer_eviction.cpp:16
setup_common.h
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
EvictionProtection3Networks050Candidates
static void EvictionProtection3Networks050Candidates(benchmark::Bench &bench)
Definition: peer_eviction.cpp:84
NET_I2P
@ NET_I2P
I2P.
Definition: netaddress.h:59
random.h
NodeEvictionCandidate
Definition: net.h:1279
NodeEvictionCandidate::id
NodeId id
Definition: net.h:1281
net.h
netaddress.h
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
bench.h
EvictionProtection3Networks250Candidates
static void EvictionProtection3Networks250Candidates(benchmark::Bench &bench)
Definition: peer_eviction.cpp:120
NodeEvictionCandidate::m_is_local
bool m_is_local
Definition: net.h:1291
GetRandomNodeEvictionCandidates
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:44
EvictionProtection1Networks250Candidates
static void EvictionProtection1Networks250Candidates(benchmark::Bench &bench)
Definition: peer_eviction.cpp:50
EvictionProtection2Networks250Candidates
static void EvictionProtection2Networks250Candidates(benchmark::Bench &bench)
Definition: peer_eviction.cpp:66
NET_ONION
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
ProtectEvictionCandidatesByRatio
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
Definition: net.cpp:914
EvictionProtection3Networks100Candidates
static void EvictionProtection3Networks100Candidates(benchmark::Bench &bench)
Definition: peer_eviction.cpp:102
NodeEvictionCandidate::m_connected
std::chrono::seconds m_connected
Definition: net.h:1282
FastRandomContext
Fast randomness source.
Definition: random.h:119
EvictionProtection0Networks250Candidates
static void EvictionProtection0Networks250Candidates(benchmark::Bench &bench)
Definition: peer_eviction.cpp:39