Bitcoin ABC  0.29.2
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 
10 #include <test/util/net.h>
11 #include <test/util/setup_common.h>
12 
13 #include <algorithm>
14 #include <functional>
15 #include <vector>
16 
18  benchmark::Bench &bench, int num_candidates,
19  std::function<void(NodeEvictionCandidate &)> candidate_setup_fn) {
20  using Candidates = std::vector<NodeEvictionCandidate>;
21  FastRandomContext random_context{true};
22 
23  Candidates candidates{
24  GetRandomNodeEvictionCandidates(num_candidates, random_context)};
25  for (auto &c : candidates) {
26  candidate_setup_fn(c);
27  }
28 
29  bench.run([&] {
30  // creating a copy has an overhead of about 3%, so it does not
31  // influence the benchmark results much.
32  auto copy = candidates;
34  });
35 }
36 
37 /* Benchmarks */
38 
40  EvictionProtectionCommon(bench, 250 /* num_candidates */,
41  [](NodeEvictionCandidate &c) {
42  c.m_connected = std::chrono::seconds{c.id};
43  c.m_network = NET_IPV4;
44  });
45 }
46 
48  EvictionProtectionCommon(bench, 250 /* num_candidates */,
49  [](NodeEvictionCandidate &c) {
50  c.m_connected = std::chrono::seconds{c.id};
51  c.m_is_local = false;
52  // 110 Tor
53  if (c.id >= 130 && c.id < 240) {
54  c.m_network = NET_ONION;
55  } else {
56  c.m_network = NET_IPV4;
57  }
58  });
59 }
60 
62  EvictionProtectionCommon(bench, 250 /* num_candidates */,
63  [](NodeEvictionCandidate &c) {
64  c.m_connected = std::chrono::seconds{c.id};
65  c.m_is_local = false;
66  if (c.id >= 90 && c.id < 160) {
67  // 70 Tor
68  c.m_network = NET_ONION;
69  } else if (c.id >= 170 && c.id < 250) {
70  // 80 I2P
71  c.m_network = NET_I2P;
72  } else {
73  c.m_network = NET_IPV4;
74  }
75  });
76 }
77 
79  EvictionProtectionCommon(bench, 50 /* num_candidates */,
80  [](NodeEvictionCandidate &c) {
81  c.m_connected = std::chrono::seconds{c.id};
82  // 2 localhost
83  c.m_is_local = (c.id == 28 || c.id == 47);
84  if (c.id >= 30 && c.id < 47) {
85  // 17 I2P
86  c.m_network = NET_I2P;
87  } else if (c.id >= 24 && c.id < 28) {
88  // 4 Tor
89  c.m_network = NET_ONION;
90  } else {
91  c.m_network = NET_IPV4;
92  }
93  });
94 }
95 
97  EvictionProtectionCommon(bench, 100 /* num_candidates */,
98  [](NodeEvictionCandidate &c) {
99  c.m_connected = std::chrono::seconds{c.id};
100  // 5 localhost
101  c.m_is_local = (c.id >= 55 && c.id < 60);
102  if (c.id >= 70 && c.id < 80) {
103  // 10 I2P
104  c.m_network = NET_I2P;
105  } else if (c.id >= 80 && c.id < 96) {
106  // 16 Tor
107  c.m_network = NET_ONION;
108  } else {
109  c.m_network = NET_IPV4;
110  }
111  });
112 }
113 
115  EvictionProtectionCommon(bench, 250 /* num_candidates */,
116  [](NodeEvictionCandidate &c) {
117  c.m_connected = std::chrono::seconds{c.id};
118  // 20 localhost
119  c.m_is_local = (c.id >= 140 && c.id < 160);
120  if (c.id >= 170 && c.id < 180) {
121  // 10 I2P
122  c.m_network = NET_I2P;
123  } else if (c.id >= 190 && c.id < 240) {
124  // 50 Tor
125  c.m_network = NET_ONION;
126  } else {
127  c.m_network = NET_IPV4;
128  }
129  });
130 }
131 
132 // Candidate numbers used for the benchmarks:
133 // - 50 candidates simulates a possible use of -maxconnections
134 // - 100 candidates approximates an average Bitcoin Core node with default
135 // settings
136 // - 250 candidates is the number of peers reported by operators of busy Bitcoin
137 // Core nodes
138 
139 // No disadvantaged networks, with 250 eviction candidates.
141 
142 // 1 disadvantaged network (Tor) with 250 eviction candidates.
144 
145 // 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates.
147 
148 // 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction
149 // candidates.
Fast randomness source.
Definition: random.h:156
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
Definition: net.cpp:1019
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
static void EvictionProtection2Networks250Candidates(benchmark::Bench &bench)
BENCHMARK(EvictionProtection0Networks250Candidates)
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)
Network m_network
Definition: net.h:1373
std::chrono::seconds m_connected
Definition: net.h:1362