Bitcoin ABC  0.26.3
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  bench.warmup(100).epochIterations(1100);
23 
24  Candidates candidates{
25  GetRandomNodeEvictionCandidates(num_candidates, random_context)};
26  for (auto &c : candidates) {
27  candidate_setup_fn(c);
28  }
29 
30  std::vector<Candidates> copies{bench.epochs() * bench.epochIterations(),
31  candidates};
32  size_t i{0};
33  bench.run([&] {
35  ++i;
36  });
37 }
38 
39 /* Benchmarks */
40 
42  EvictionProtectionCommon(bench, 250 /* num_candidates */,
43  [](NodeEvictionCandidate &c) {
44  c.m_connected = std::chrono::seconds{c.id};
45  c.m_network = NET_IPV4;
46  });
47 }
48 
50  EvictionProtectionCommon(bench, 250 /* num_candidates */,
51  [](NodeEvictionCandidate &c) {
52  c.m_connected = std::chrono::seconds{c.id};
53  c.m_is_local = false;
54  // 110 Tor
55  if (c.id >= 130 && c.id < 240) {
56  c.m_network = NET_ONION;
57  } else {
58  c.m_network = NET_IPV4;
59  }
60  });
61 }
62 
64  EvictionProtectionCommon(bench, 250 /* num_candidates */,
65  [](NodeEvictionCandidate &c) {
66  c.m_connected = std::chrono::seconds{c.id};
67  c.m_is_local = false;
68  if (c.id >= 90 && c.id < 160) {
69  // 70 Tor
70  c.m_network = NET_ONION;
71  } else if (c.id >= 170 && c.id < 250) {
72  // 80 I2P
73  c.m_network = NET_I2P;
74  } else {
75  c.m_network = NET_IPV4;
76  }
77  });
78 }
79 
81  EvictionProtectionCommon(bench, 50 /* num_candidates */,
82  [](NodeEvictionCandidate &c) {
83  c.m_connected = std::chrono::seconds{c.id};
84  // 2 localhost
85  c.m_is_local = (c.id == 28 || c.id == 47);
86  if (c.id >= 30 && c.id < 47) {
87  // 17 I2P
88  c.m_network = NET_I2P;
89  } else if (c.id >= 24 && c.id < 28) {
90  // 4 Tor
91  c.m_network = NET_ONION;
92  } else {
93  c.m_network = NET_IPV4;
94  }
95  });
96 }
97 
99  EvictionProtectionCommon(bench, 100 /* num_candidates */,
100  [](NodeEvictionCandidate &c) {
101  c.m_connected = std::chrono::seconds{c.id};
102  // 5 localhost
103  c.m_is_local = (c.id >= 55 && c.id < 60);
104  if (c.id >= 70 && c.id < 80) {
105  // 10 I2P
106  c.m_network = NET_I2P;
107  } else if (c.id >= 80 && c.id < 96) {
108  // 16 Tor
109  c.m_network = NET_ONION;
110  } else {
111  c.m_network = NET_IPV4;
112  }
113  });
114 }
115 
117  EvictionProtectionCommon(bench, 250 /* num_candidates */,
118  [](NodeEvictionCandidate &c) {
119  c.m_connected = std::chrono::seconds{c.id};
120  // 20 localhost
121  c.m_is_local = (c.id >= 140 && c.id < 160);
122  if (c.id >= 170 && c.id < 180) {
123  // 10 I2P
124  c.m_network = NET_I2P;
125  } else if (c.id >= 190 && c.id < 240) {
126  // 50 Tor
127  c.m_network = NET_ONION;
128  } else {
129  c.m_network = NET_IPV4;
130  }
131  });
132 }
133 
134 // Candidate numbers used for the benchmarks:
135 // - 50 candidates simulates a possible use of -maxconnections
136 // - 100 candidates approximates an average Bitcoin Core node with default
137 // settings
138 // - 250 candidates is the number of peers reported by operators of busy Bitcoin
139 // Core nodes
140 
141 // No disadvantaged networks, with 250 eviction candidates.
143 
144 // 1 disadvantaged network (Tor) with 250 eviction candidates.
146 
147 // 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates.
149 
150 // 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction
151 // candidates.
Fast randomness source.
Definition: random.h:129
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
Bench & epochIterations(uint64_t numIters) noexcept
Sets exactly the number of iterations for each epoch.
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
Bench & warmup(uint64_t numWarmupIters) noexcept
Sets a number of iterations that are initially performed without any measurements.
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
Definition: net.cpp:1011
@ NET_I2P
I2P.
Definition: netaddress.h:58
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:55
@ NET_IPV4
IPv4.
Definition: netaddress.h:49
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:1474
std::chrono::seconds m_connected
Definition: net.h:1463