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>
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 */,
45 c.nTimeConnected = c.id;
47 });
48}
49
51{
53 bench,
54 250 /* num_candidates */,
56 c.nTimeConnected = 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 */,
72 c.nTimeConnected = 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 */,
90 c.nTimeConnected = 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
94 } else if (c.id >= 24 && c.id < 28) { // 4 Tor
96 } else {
98 }
99 });
100}
101
103{
105 bench,
106 100 /* num_candidates */,
107 [](NodeEvictionCandidate& c) {
108 c.nTimeConnected = 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
114 } else {
116 }
117 });
118}
119
121{
123 bench,
124 250 /* num_candidates */,
125 [](NodeEvictionCandidate& c) {
126 c.nTimeConnected = 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
132 } else {
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.
Fast randomness source.
Definition: random.h:120
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
if(na.IsAddrV1Compatible())
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
Definition: net.cpp:913
@ 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:1203
int64_t nTimeConnected
Definition: net.h:1193
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:44