|
15 | 15 |
|
16 | 16 | BOOST_FIXTURE_TEST_SUITE(net_peer_eviction_tests, BasicTestingSetup) |
17 | 17 |
|
18 | | -namespace { |
19 | | -constexpr int NODE_EVICTION_TEST_ROUNDS{10}; |
20 | | -constexpr int NODE_EVICTION_TEST_UP_TO_N_NODES{200}; |
21 | | -} // namespace |
22 | | - |
23 | 18 | std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_candidates, FastRandomContext& random_context) |
24 | 19 | { |
25 | 20 | std::vector<NodeEvictionCandidate> candidates; |
@@ -257,91 +252,89 @@ BOOST_AUTO_TEST_CASE(peer_eviction_test) |
257 | 252 | { |
258 | 253 | FastRandomContext random_context{true}; |
259 | 254 |
|
260 | | - for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) { |
261 | | - for (int number_of_nodes = 0; number_of_nodes < NODE_EVICTION_TEST_UP_TO_N_NODES; ++number_of_nodes) { |
262 | | - // Four nodes with the highest keyed netgroup values should be |
263 | | - // protected from eviction. |
264 | | - BOOST_CHECK(!IsEvicted( |
265 | | - number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
266 | | - candidate.nKeyedNetGroup = number_of_nodes - candidate.id; |
267 | | - }, |
268 | | - {0, 1, 2, 3}, random_context)); |
269 | | - |
270 | | - // Eight nodes with the lowest minimum ping time should be protected |
271 | | - // from eviction. |
272 | | - BOOST_CHECK(!IsEvicted( |
273 | | - number_of_nodes, [](NodeEvictionCandidate& candidate) { |
274 | | - candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; |
275 | | - }, |
276 | | - {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
277 | | - |
278 | | - // Four nodes that most recently sent us novel transactions accepted |
279 | | - // into our mempool should be protected from eviction. |
280 | | - BOOST_CHECK(!IsEvicted( |
281 | | - number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
282 | | - candidate.nLastTXTime = number_of_nodes - candidate.id; |
283 | | - }, |
284 | | - {0, 1, 2, 3}, random_context)); |
285 | | - |
286 | | - // Up to eight non-tx-relay peers that most recently sent us novel |
287 | | - // blocks should be protected from eviction. |
288 | | - BOOST_CHECK(!IsEvicted( |
289 | | - number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
290 | | - candidate.nLastBlockTime = number_of_nodes - candidate.id; |
291 | | - if (candidate.id <= 7) { |
292 | | - candidate.fRelayTxes = false; |
293 | | - candidate.fRelevantServices = true; |
294 | | - } |
295 | | - }, |
296 | | - {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
297 | | - |
298 | | - // Four peers that most recently sent us novel blocks should be |
299 | | - // protected from eviction. |
300 | | - BOOST_CHECK(!IsEvicted( |
301 | | - number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
302 | | - candidate.nLastBlockTime = number_of_nodes - candidate.id; |
303 | | - }, |
304 | | - {0, 1, 2, 3}, random_context)); |
305 | | - |
306 | | - // Combination of the previous two tests. |
307 | | - BOOST_CHECK(!IsEvicted( |
308 | | - number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
309 | | - candidate.nLastBlockTime = number_of_nodes - candidate.id; |
310 | | - if (candidate.id <= 7) { |
311 | | - candidate.fRelayTxes = false; |
312 | | - candidate.fRelevantServices = true; |
313 | | - } |
314 | | - }, |
315 | | - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, random_context)); |
316 | | - |
317 | | - // Combination of all tests above. |
318 | | - BOOST_CHECK(!IsEvicted( |
319 | | - number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
320 | | - candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected |
321 | | - candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; // 8 protected |
322 | | - candidate.nLastTXTime = number_of_nodes - candidate.id; // 4 protected |
323 | | - candidate.nLastBlockTime = number_of_nodes - candidate.id; // 4 protected |
324 | | - }, |
325 | | - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context)); |
326 | | - |
327 | | - // An eviction is expected given >= 29 random eviction candidates. The eviction logic protects at most |
328 | | - // four peers by net group, eight by lowest ping time, four by last time of novel tx, up to eight non-tx-relay |
329 | | - // peers by last novel block time, and four more peers by last novel block time. |
330 | | - if (number_of_nodes >= 29) { |
331 | | - BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
332 | | - } |
333 | | - |
334 | | - // No eviction is expected given <= 20 random eviction candidates. The eviction logic protects at least |
335 | | - // four peers by net group, eight by lowest ping time, four by last time of novel tx and four peers by last |
336 | | - // novel block time. |
337 | | - if (number_of_nodes <= 20) { |
338 | | - BOOST_CHECK(!SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
339 | | - } |
340 | | - |
341 | | - // Cases left to test: |
342 | | - // * "If any remaining peers are preferred for eviction consider only them. [...]" |
343 | | - // * "Identify the network group with the most connections and youngest member. [...]" |
| 255 | + for (int number_of_nodes = 0; number_of_nodes < 200; ++number_of_nodes) { |
| 256 | + // Four nodes with the highest keyed netgroup values should be |
| 257 | + // protected from eviction. |
| 258 | + BOOST_CHECK(!IsEvicted( |
| 259 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 260 | + candidate.nKeyedNetGroup = number_of_nodes - candidate.id; |
| 261 | + }, |
| 262 | + {0, 1, 2, 3}, random_context)); |
| 263 | + |
| 264 | + // Eight nodes with the lowest minimum ping time should be protected |
| 265 | + // from eviction. |
| 266 | + BOOST_CHECK(!IsEvicted( |
| 267 | + number_of_nodes, [](NodeEvictionCandidate& candidate) { |
| 268 | + candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; |
| 269 | + }, |
| 270 | + {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
| 271 | + |
| 272 | + // Four nodes that most recently sent us novel transactions accepted |
| 273 | + // into our mempool should be protected from eviction. |
| 274 | + BOOST_CHECK(!IsEvicted( |
| 275 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 276 | + candidate.nLastTXTime = number_of_nodes - candidate.id; |
| 277 | + }, |
| 278 | + {0, 1, 2, 3}, random_context)); |
| 279 | + |
| 280 | + // Up to eight non-tx-relay peers that most recently sent us novel |
| 281 | + // blocks should be protected from eviction. |
| 282 | + BOOST_CHECK(!IsEvicted( |
| 283 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 284 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 285 | + if (candidate.id <= 7) { |
| 286 | + candidate.fRelayTxes = false; |
| 287 | + candidate.fRelevantServices = true; |
| 288 | + } |
| 289 | + }, |
| 290 | + {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
| 291 | + |
| 292 | + // Four peers that most recently sent us novel blocks should be |
| 293 | + // protected from eviction. |
| 294 | + BOOST_CHECK(!IsEvicted( |
| 295 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 296 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 297 | + }, |
| 298 | + {0, 1, 2, 3}, random_context)); |
| 299 | + |
| 300 | + // Combination of the previous two tests. |
| 301 | + BOOST_CHECK(!IsEvicted( |
| 302 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 303 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 304 | + if (candidate.id <= 7) { |
| 305 | + candidate.fRelayTxes = false; |
| 306 | + candidate.fRelevantServices = true; |
| 307 | + } |
| 308 | + }, |
| 309 | + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, random_context)); |
| 310 | + |
| 311 | + // Combination of all tests above. |
| 312 | + BOOST_CHECK(!IsEvicted( |
| 313 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 314 | + candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected |
| 315 | + candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; // 8 protected |
| 316 | + candidate.nLastTXTime = number_of_nodes - candidate.id; // 4 protected |
| 317 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; // 4 protected |
| 318 | + }, |
| 319 | + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context)); |
| 320 | + |
| 321 | + // An eviction is expected given >= 29 random eviction candidates. The eviction logic protects at most |
| 322 | + // four peers by net group, eight by lowest ping time, four by last time of novel tx, up to eight non-tx-relay |
| 323 | + // peers by last novel block time, and four more peers by last novel block time. |
| 324 | + if (number_of_nodes >= 29) { |
| 325 | + BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
| 326 | + } |
| 327 | + |
| 328 | + // No eviction is expected given <= 20 random eviction candidates. The eviction logic protects at least |
| 329 | + // four peers by net group, eight by lowest ping time, four by last time of novel tx and four peers by last |
| 330 | + // novel block time. |
| 331 | + if (number_of_nodes <= 20) { |
| 332 | + BOOST_CHECK(!SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
344 | 333 | } |
| 334 | + |
| 335 | + // Cases left to test: |
| 336 | + // * "If any remaining peers are preferred for eviction consider only them. [...]" |
| 337 | + // * "Identify the network group with the most connections and youngest member. [...]" |
345 | 338 | } |
346 | 339 | } |
347 | 340 |
|
|
0 commit comments