|
42 | 42 | #endif
|
43 | 43 |
|
44 | 44 | #include <algorithm>
|
| 45 | +#include <array> |
45 | 46 | #include <cstdint>
|
46 | 47 | #include <functional>
|
47 | 48 | #include <optional>
|
@@ -918,35 +919,66 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
|
918 | 919 | {
|
919 | 920 | // Protect the half of the remaining nodes which have been connected the longest.
|
920 | 921 | // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
|
921 |
| - // To favorise the diversity of our peer connections, reserve up to (half + 2) of |
922 |
| - // these protected spots for onion and localhost peers, if any, even if they're not |
923 |
| - // longest uptime overall. This helps protect tor peers, which tend to be otherwise |
| 922 | + // To favorise the diversity of our peer connections, reserve up to half of these protected |
| 923 | + // spots for Tor/onion and localhost peers, even if they're not longest uptime overall. |
| 924 | + // This helps protect these higher-latency peers that tend to be otherwise |
924 | 925 | // disadvantaged under our eviction criteria.
|
925 | 926 | const size_t initial_size = eviction_candidates.size();
|
926 | 927 | const size_t total_protect_size{initial_size / 2};
|
927 |
| - const size_t onion_protect_size = total_protect_size / 2; |
928 | 928 |
|
929 |
| - if (onion_protect_size) { |
930 |
| - // Pick out up to 1/4 peers connected via our onion service, sorted by longest uptime. |
931 |
| - EraseLastKElements(eviction_candidates, CompareOnionTimeConnected, onion_protect_size, |
932 |
| - [](const NodeEvictionCandidate& n) { return n.m_is_onion; }); |
933 |
| - } |
934 |
| - |
935 |
| - const size_t localhost_min_protect_size{2}; |
936 |
| - if (onion_protect_size >= localhost_min_protect_size) { |
937 |
| - // Allocate any remaining slots of the 1/4, or minimum 2 additional slots, |
938 |
| - // to localhost peers, sorted by longest uptime, as manually configured |
939 |
| - // hidden services not using `-bind=addr[:port]=onion` will not be detected |
940 |
| - // as inbound onion connections. |
941 |
| - const size_t remaining_tor_slots{onion_protect_size - (initial_size - eviction_candidates.size())}; |
942 |
| - const size_t localhost_protect_size{std::max(remaining_tor_slots, localhost_min_protect_size)}; |
943 |
| - EraseLastKElements(eviction_candidates, CompareLocalHostTimeConnected, localhost_protect_size, |
944 |
| - [](const NodeEvictionCandidate& n) { return n.m_is_local; }); |
| 929 | + // Disadvantaged networks to protect: localhost and Tor/onion. In case of equal counts, earlier |
| 930 | + // array members have first opportunity to recover unused slots from the previous iteration. |
| 931 | + struct Net { bool is_local; Network id; size_t count; }; |
| 932 | + std::array<Net, 3> networks{{{/* localhost */ true, NET_MAX, 0}, {false, NET_ONION, 0}}}; |
| 933 | + |
| 934 | + // Count and store the number of eviction candidates per network. |
| 935 | + for (Net& n : networks) { |
| 936 | + n.count = std::count_if(eviction_candidates.cbegin(), eviction_candidates.cend(), |
| 937 | + [&n](const NodeEvictionCandidate& c) { |
| 938 | + return n.is_local ? c.m_is_local : c.m_network == n.id; |
| 939 | + }); |
| 940 | + } |
| 941 | + // Sort `networks` by ascending candidate count, to give networks having fewer candidates |
| 942 | + // the first opportunity to recover unused protected slots from the previous iteration. |
| 943 | + std::stable_sort(networks.begin(), networks.end(), [](Net a, Net b) { return a.count < b.count; }); |
| 944 | + |
| 945 | + // Protect up to 25% of the eviction candidates by disadvantaged network. |
| 946 | + const size_t max_protect_by_network{total_protect_size / 2}; |
| 947 | + size_t num_protected{0}; |
| 948 | + |
| 949 | + while (num_protected < max_protect_by_network) { |
| 950 | + const size_t disadvantaged_to_protect{max_protect_by_network - num_protected}; |
| 951 | + const size_t protect_per_network{ |
| 952 | + std::max(disadvantaged_to_protect / networks.size(), static_cast<size_t>(1))}; |
| 953 | + |
| 954 | + // Early exit flag if there are no remaining candidates by disadvantaged network. |
| 955 | + bool protected_at_least_one{false}; |
| 956 | + |
| 957 | + for (const Net& n : networks) { |
| 958 | + if (n.count == 0) continue; |
| 959 | + const size_t before = eviction_candidates.size(); |
| 960 | + EraseLastKElements(eviction_candidates, CompareNodeNetworkTime(n.is_local, n.id), |
| 961 | + protect_per_network, [&n](const NodeEvictionCandidate& c) { |
| 962 | + return n.is_local ? c.m_is_local : c.m_network == n.id; |
| 963 | + }); |
| 964 | + const size_t after = eviction_candidates.size(); |
| 965 | + if (before > after) { |
| 966 | + protected_at_least_one = true; |
| 967 | + num_protected += before - after; |
| 968 | + if (num_protected >= max_protect_by_network) { |
| 969 | + break; |
| 970 | + } |
| 971 | + } |
| 972 | + } |
| 973 | + if (!protected_at_least_one) { |
| 974 | + break; |
| 975 | + } |
945 | 976 | }
|
946 | 977 |
|
947 | 978 | // Calculate how many we removed, and update our total number of peers that
|
948 | 979 | // we want to protect based on uptime accordingly.
|
949 |
| - const size_t remaining_to_protect{total_protect_size - (initial_size - eviction_candidates.size())}; |
| 980 | + assert(num_protected == initial_size - eviction_candidates.size()); |
| 981 | + const size_t remaining_to_protect{total_protect_size - num_protected}; |
950 | 982 | EraseLastKElements(eviction_candidates, ReverseCompareNodeTimeConnected, remaining_to_protect);
|
951 | 983 | }
|
952 | 984 |
|
|
0 commit comments