Inside Bitcoin
Opening the selected file…
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) 2022-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#ifndef BITCOIN_NODE_EVICTION_H6#define BITCOIN_NODE_EVICTION_H8#include <node/connection_types.h>9#include <net_permissions.h>11#include <chrono>12#include <cstdint>13#include <optional>14#include <vector>16typedef int64_t NodeId;18struct NodeEvictionCandidate {19NodeId id;20NodeClock::time_point m_connected;21NodeClock::duration m_min_ping_time;22std::chrono::seconds m_last_block_time;23std::chrono::seconds m_last_tx_time;24bool fRelevantServices;25bool m_relay_txs;26bool fBloomFilter;27uint64_t nKeyedNetGroup;28bool prefer_evict;29bool m_is_local;30Network m_network;31bool m_noban;32ConnectionType m_conn_type;33};35/**36* Select an inbound peer to evict after filtering out (protecting) peers having37* distinct, difficult-to-forge characteristics. The protection logic picks out38* fixed numbers of desirable peers per various criteria, followed by (mostly)39* ratios of desirable or disadvantaged peers. If any eviction candidates40* remain, the selection logic chooses a peer to evict.41*/42[[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates);44/** Protect desirable or disadvantaged inbound peers from eviction by ratio.45*46* This function protects half of the peers which have been connected the47* longest, to replicate the non-eviction implicit behavior and preclude attacks48* that start later.49*50* Half of these protected spots (1/4 of the total) are reserved for the51* following categories of peers, sorted by longest uptime, even if they're not52* longest uptime overall:53*54* - onion peers connected via our tor control service55*56* - localhost peers, as manually configured hidden services not using57* `-bind=addr[:port]=onion` will not be detected as inbound onion connections58*59* - I2P peers60*61* - CJDNS peers62*63* This helps protect these privacy network peers, which tend to be otherwise64* disadvantaged under our eviction criteria for their higher min ping times65* relative to IPv4/IPv6 peers, and favorise the diversity of peer connections.66*/67void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& vEvictionCandidates);69#endif // BITCOIN_NODE_EVICTION_H