Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.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#include <util/time.h>12#include <functional>13#include <vector>15static void EvictionProtectionCommon(16benchmark::Bench& bench,17int num_candidates,18std::function<void(NodeEvictionCandidate&)> candidate_setup_fn)19{20using Candidates = std::vector<NodeEvictionCandidate>;21FastRandomContext random_context{true};23Candidates candidates{GetRandomNodeEvictionCandidates(num_candidates, random_context)};24for (auto& c : candidates) {25candidate_setup_fn(c);26}29bench.run([&] {30// creating a copy has an overhead of about 3%, so it does not influence the benchmark results much.31auto copy = candidates;32ProtectEvictionCandidatesByRatio(copy);33});34}36/* Benchmarks */38static void EvictionProtection0Networks250Candidates(benchmark::Bench& bench)39{40EvictionProtectionCommon(41bench,42/*num_candidates=*/250,43[](NodeEvictionCandidate& c) {44c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};45c.m_network = NET_IPV4;46});47}49static void EvictionProtection1Networks250Candidates(benchmark::Bench& bench)50{51EvictionProtectionCommon(52bench,53/*num_candidates=*/250,54[](NodeEvictionCandidate& c) {55c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};56c.m_is_local = false;57if (c.id >= 130 && c.id < 240) { // 110 Tor58c.m_network = NET_ONION;59} else {60c.m_network = NET_IPV4;61}62});63}65static void EvictionProtection2Networks250Candidates(benchmark::Bench& bench)66{67EvictionProtectionCommon(68bench,69/*num_candidates=*/250,70[](NodeEvictionCandidate& c) {71c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};72c.m_is_local = false;73if (c.id >= 90 && c.id < 160) { // 70 Tor74c.m_network = NET_ONION;75} else if (c.id >= 170 && c.id < 250) { // 80 I2P76c.m_network = NET_I2P;77} else {78c.m_network = NET_IPV4;79}80});81}83static void EvictionProtection3Networks050Candidates(benchmark::Bench& bench)84{85EvictionProtectionCommon(86bench,87/*num_candidates=*/50,88[](NodeEvictionCandidate& c) {89c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};90c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost91if (c.id >= 30 && c.id < 47) { // 17 I2P92c.m_network = NET_I2P;93} else if (c.id >= 24 && c.id < 28) { // 4 Tor94c.m_network = NET_ONION;95} else {96c.m_network = NET_IPV4;97}98});99}101static void EvictionProtection3Networks100Candidates(benchmark::Bench& bench)102{103EvictionProtectionCommon(104bench,105/*num_candidates=*/100,106[](NodeEvictionCandidate& c) {107c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};108c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost109if (c.id >= 70 && c.id < 80) { // 10 I2P110c.m_network = NET_I2P;111} else if (c.id >= 80 && c.id < 96) { // 16 Tor112c.m_network = NET_ONION;113} else {114c.m_network = NET_IPV4;115}116});117}119static void EvictionProtection3Networks250Candidates(benchmark::Bench& bench)120{121EvictionProtectionCommon(122bench,123/*num_candidates=*/250,124[](NodeEvictionCandidate& c) {125c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};126c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost127if (c.id >= 170 && c.id < 180) { // 10 I2P128c.m_network = NET_I2P;129} else if (c.id >= 190 && c.id < 240) { // 50 Tor130c.m_network = NET_ONION;131} else {132c.m_network = NET_IPV4;133}134});135}137// Candidate numbers used for the benchmarks:138// - 50 candidates simulates a possible use of -maxconnections139// - 100 candidates approximates an average node with default settings140// - 250 candidates is the number of peers reported by operators of busy nodes142// No disadvantaged networks, with 250 eviction candidates.143BENCHMARK(EvictionProtection0Networks250Candidates);145// 1 disadvantaged network (Tor) with 250 eviction candidates.146BENCHMARK(EvictionProtection1Networks250Candidates);148// 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates.149BENCHMARK(EvictionProtection2Networks250Candidates);151// 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction candidates.152BENCHMARK(EvictionProtection3Networks050Candidates);153BENCHMARK(EvictionProtection3Networks100Candidates);154BENCHMARK(EvictionProtection3Networks250Candidates);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.