Bitcoin Core 28.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 <netaddress.h>
7#include <node/eviction.h>
8#include <random.h>
9#include <test/util/net.h>
10
11#include <chrono>
12#include <functional>
13#include <vector>
14
16 benchmark::Bench& bench,
17 int num_candidates,
18 std::function<void(NodeEvictionCandidate&)> candidate_setup_fn)
19{
20 using Candidates = std::vector<NodeEvictionCandidate>;
21 FastRandomContext random_context{true};
22
23 Candidates candidates{GetRandomNodeEvictionCandidates(num_candidates, random_context)};
24 for (auto& c : candidates) {
25 candidate_setup_fn(c);
26 }
27
28
29 bench.run([&] {
30 // creating a copy has an overhead of about 3%, so it does not influence the benchmark results much.
31 auto copy = candidates;
33 });
34}
35
36/* Benchmarks */
37
39{
41 bench,
42 /*num_candidates=*/250,
44 c.m_connected = std::chrono::seconds{c.id};
46 });
47}
48
50{
52 bench,
53 /*num_candidates=*/250,
55 c.m_connected = std::chrono::seconds{c.id};
56 c.m_is_local = false;
57 if (c.id >= 130 && c.id < 240) { // 110 Tor
58 c.m_network = NET_ONION;
59 } else {
60 c.m_network = NET_IPV4;
61 }
62 });
63}
64
66{
68 bench,
69 /*num_candidates=*/250,
71 c.m_connected = std::chrono::seconds{c.id};
72 c.m_is_local = false;
73 if (c.id >= 90 && c.id < 160) { // 70 Tor
74 c.m_network = NET_ONION;
75 } else if (c.id >= 170 && c.id < 250) { // 80 I2P
76 c.m_network = NET_I2P;
77 } else {
78 c.m_network = NET_IPV4;
79 }
80 });
81}
82
84{
86 bench,
87 /*num_candidates=*/50,
89 c.m_connected = std::chrono::seconds{c.id};
90 c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost
91 if (c.id >= 30 && c.id < 47) { // 17 I2P
93 } else if (c.id >= 24 && c.id < 28) { // 4 Tor
95 } else {
97 }
98 });
99}
100
102{
104 bench,
105 /*num_candidates=*/100,
106 [](NodeEvictionCandidate& c) {
107 c.m_connected = std::chrono::seconds{c.id};
108 c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost
109 if (c.id >= 70 && c.id < 80) { // 10 I2P
110 c.m_network = NET_I2P;
111 } else if (c.id >= 80 && c.id < 96) { // 16 Tor
113 } else {
115 }
116 });
117}
118
120{
122 bench,
123 /*num_candidates=*/250,
124 [](NodeEvictionCandidate& c) {
125 c.m_connected = std::chrono::seconds{c.id};
126 c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost
127 if (c.id >= 170 && c.id < 180) { // 10 I2P
128 c.m_network = NET_I2P;
129 } else if (c.id >= 190 && c.id < 240) { // 50 Tor
131 } else {
133 }
134 });
135}
136
137// Candidate numbers used for the benchmarks:
138// - 50 candidates simulates a possible use of -maxconnections
139// - 100 candidates approximates an average node with default settings
140// - 250 candidates is the number of peers reported by operators of busy nodes
141
142// No disadvantaged networks, with 250 eviction candidates.
144
145// 1 disadvantaged network (Tor) with 250 eviction candidates.
147
148// 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates.
150
151// 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction candidates.
if(!SetupNetworking())
Fast randomness source.
Definition: random.h:377
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:48
@ 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:116