Skip to content
Bitcoin Roots

v29.4-roots.4 · Commit 3: [PATCH 03/37] 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. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/node/eviction.cpp CHANGED
@@ -3,6 +3,7 @@
3
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
 
5
  #include <node/eviction.h>
 
6
 
7
  #include <algorithm>
8
  #include <array>
@@ -175,7 +176,7 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
175
  EraseLastKElements(eviction_candidates, ReverseCompareNodeTimeConnected, remaining_to_protect);
176
  }
177
 
178
- [[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates)
179
  {
180
  // Protect connections with certain characteristics
181
 
@@ -183,6 +184,15 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
183
 
184
  ProtectOutboundConnections(vEvictionCandidates);
185
 
 
 
 
 
 
 
 
 
 
186
  // Deterministically select 4 peers to protect by netgroup.
187
  // An attacker cannot predict which netgroups will be protected
188
  EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
@@ -204,7 +214,8 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
204
  // or disadvantaged characteristics.
205
  ProtectEvictionCandidatesByRatio(vEvictionCandidates);
206
 
207
- if (vEvictionCandidates.empty()) return std::nullopt;
 
208
 
209
  // If any remaining peers are preferred for eviction consider only them.
210
  // This happens after the other preferences since if a peer is really the best by other criteria (esp relaying blocks)
 
3
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
 
5
  #include <node/eviction.h>
6
+ #include <random.h>
7
 
8
  #include <algorithm>
9
  #include <array>
 
176
  EraseLastKElements(eviction_candidates, ReverseCompareNodeTimeConnected, remaining_to_protect);
177
  }
178
 
179
+ [[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates, bool force)
180
  {
181
  // Protect connections with certain characteristics
182
 
 
184
 
185
  ProtectOutboundConnections(vEvictionCandidates);
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
+
196
  // Deterministically select 4 peers to protect by netgroup.
197
  // An attacker cannot predict which netgroups will be protected
198
  EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
 
214
  // or disadvantaged characteristics.
215
  ProtectEvictionCandidatesByRatio(vEvictionCandidates);
216
 
217
+ // May still return nullopt is `force` argument is false
218
+ if (vEvictionCandidates.empty()) return force_evict;
219
 
220
  // If any remaining peers are preferred for eviction consider only them.
221
  // This happens after the other preferences since if a peer is really the best by other criteria (esp relaying blocks)