aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
authorJon Atack <jon@atack.com>2021-06-19 17:19:38 +0200
committerJon Atack <jon@atack.com>2021-07-08 12:28:35 +0200
commit02e411ec456af80d1da76085a814c68bb3aca6de (patch)
treee0ededf60f9f9734d6388e5df90f1634561afa1e /src/net.cpp
parent5adb06457403f8c1d874e9c6748ecbb78ef8fa2b (diff)
p2p: iterate eviction protection only on networks having candidates
in ProtectEvictionCandidatesByRatio(). Thank you to Vasil Dimov, whose suggestions during a post-merge discussion about PR 21261 reminded me that I had done this in earlier versions of the PR, e.g. commits like ef411cd2. Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 60059249ed..1611b1a24b 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -935,7 +935,10 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
const size_t max_protect_by_network{total_protect_size / 2};
size_t num_protected{0};
- while (num_protected < max_protect_by_network) {
+ // Count the number of disadvantaged networks from which we have peers to protect.
+ auto num_networks = std::count_if(networks.begin(), networks.end(), [](const Net& n) { return n.count; });
+
+ while (num_networks != 0 && num_protected < max_protect_by_network) {
const size_t disadvantaged_to_protect{max_protect_by_network - num_protected};
const size_t protect_per_network{
std::max(disadvantaged_to_protect / networks.size(), static_cast<size_t>(1))};