v29.4-roots.3 · Commit 3: [PATCH 03/30] policy: restore conservative relay and network controls
src/node/eviction.cpp
Before: src/node/eviction.cpp · After: src/node/eviction.cpp
File metadata
index 33406931d4934c35054ab262a5ed083756ea274d..7c346920265e73bc24a9e22eb0b8c7e9abe9bf8a 100644
Download original patch · Release notes
Diff rows 1–39 of 39. Line numbers refer to the original before/after files.
+ Added− RemovedUnmarked lines are context.
Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.
src/node/eviction.cpp
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
4
|
|
|
5
5
|
#include <node/eviction.h>
|
|
6
|
+
#include <random.h>
|
|
6
7
|
|
|
7
8
|
#include <algorithm>
|
|
8
9
|
#include <array>
|
|
@@ -175,7 +176,7 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
|
|
|
175
176
|
EraseLastKElements(eviction_candidates, ReverseCompareNodeTimeConnected, remaining_to_protect);
|
|
176
177
|
}
|
|
177
178
|
|
|
178
|
-
[[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates)
|
|
179
|
+
[[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates, bool force)
|
|
179
180
|
{
|
|
180
181
|
// Protect connections with certain characteristics
|
|
181
182
|
|
|
@@ -183,6 +184,15 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
|
|
|
183
184
|
|
|
184
185
|
ProtectOutboundConnections(vEvictionCandidates);
|
|
185
186
|
|
|
187
|
+
if (vEvictionCandidates.empty()) return std::nullopt;
|
|
188
|
+
|
|
189
|
+
// Hang on to one random node to evict if forced
|
|
190
|
+
std::optional<NodeId> force_evict;
|
|
191
|
+
if (force) {
|
|
192
|
+
uint64_t randpos{FastRandomContext().randrange(vEvictionCandidates.size())};
|
|
193
|
+
force_evict = vEvictionCandidates.at(randpos).id;
|
|
194
|
+
}
|
|
195
|
+
|
|
186
196
|
// Deterministically select 4 peers to protect by netgroup.
|
|
187
197
|
// An attacker cannot predict which netgroups will be protected
|
|
188
198
|
EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
|
|
@@ -204,7 +214,8 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
|
|
|
204
214
|
// or disadvantaged characteristics.
|
|
205
215
|
ProtectEvictionCandidatesByRatio(vEvictionCandidates);
|
|
206
216
|
|
|
207
|
-
if (vEvictionCandidates.empty()) return std::nullopt;
|
|
217
|
+
// May still return nullopt is `force` argument is false
|
|
218
|
+
if (vEvictionCandidates.empty()) return force_evict;
|
|
208
219
|
|
|
209
220
|
// If any remaining peers are preferred for eviction consider only them.
|
|
210
221
|
// This happens after the other preferences since if a peer is really the best by other criteria (esp relaying blocks)
|