eviction.h raw

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