1 // Copyright (c) 2022 The Limenka 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 LIMENKA_NODE_EVICTION_H
6 #define LIMENKA_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 std::chrono::seconds m_connected;
21 std::chrono::microseconds 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 * @param[in] force Attempt to evict a random peer if no candidates
42 * are available among inbound no-ban connections.
43 * (see CConman::AttemptToEvictConnection())
44 * Default: false
45 */
46 [[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates, bool force = false);
47 48 /** Protect desirable or disadvantaged inbound peers from eviction by ratio.
49 *
50 * This function protects half of the peers which have been connected the
51 * longest, to replicate the non-eviction implicit behavior and preclude attacks
52 * that start later.
53 *
54 * Half of these protected spots (1/4 of the total) are reserved for the
55 * following categories of peers, sorted by longest uptime, even if they're not
56 * longest uptime overall:
57 *
58 * - onion peers connected via our tor control service
59 *
60 * - localhost peers, as manually configured hidden services not using
61 * `-bind=addr[:port]=onion` will not be detected as inbound onion connections
62 *
63 * - I2P peers
64 *
65 * - CJDNS peers
66 *
67 * This helps protect these privacy network peers, which tend to be otherwise
68 * disadvantaged under our eviction criteria for their higher min ping times
69 * relative to IPv4/IPv6 peers, and favorise the diversity of peer connections.
70 */
71 void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& vEvictionCandidates);
72 73 #endif // LIMENKA_NODE_EVICTION_H
74