10#include <boost/test/unit_test.hpp>
15#include <unordered_set>
26 const
std::unordered_set<
NodeId>& protected_peer_ids,
27 const
std::unordered_set<
NodeId>& unprotected_peer_ids,
32 candidate_setup_fn(candidate);
34 std::shuffle(candidates.begin(), candidates.end(), random_context);
36 const size_t size{candidates.size()};
37 const size_t expected{size - size / 2};
41 size_t unprotected_count{0};
43 if (protected_peer_ids.count(candidate.id)) {
45 BOOST_TEST_MESSAGE(
strprintf(
"expected candidate to be protected: %d", candidate.id));
48 if (unprotected_peer_ids.count(candidate.id)) {
54 const bool is_protected{unprotected_count == unprotected_peer_ids.size()};
56 BOOST_TEST_MESSAGE(
strprintf(
"unprotected: expected %d, actual %d",
57 unprotected_peer_ids.size(), unprotected_count));
112 {4, 5, 6, 7, 10, 11},
135 {3, 4, 5, 6, 10, 11},
235 {3, 4, 5, 8, 10, 11},
258 {0, 1, 2, 3, 6, 8, 9, 10},
259 {4, 5, 7, 11, 12, 13, 14, 15},
271 {0, 1, 2, 3, 10, 11, 12, 13},
272 {4, 5, 6, 7, 8, 9, 14, 15},
284 {0, 1, 2, 3, 7, 8, 9, 15},
285 {5, 6, 10, 11, 12, 13, 14},
295 if (c.
id == 8 || c.
id == 10) {
296 c.m_network = NET_ONION;
297 }
else if (c.
id == 6 || c.
id == 9 || c.
id == 11 || c.
id == 12) {
298 c.m_network = NET_I2P;
300 c.m_network = NET_IPV4;
317 c.m_network = NET_I2P;
318 }
else if (c.
id == 1) {
319 c.m_network = NET_ONION;
321 c.m_network = NET_IPV6;
336 c.m_network = NET_I2P;
337 }
else if (c.
id == 5) {
338 c.m_network = NET_ONION;
340 c.m_network = NET_IPV6;
355 c.m_network = NET_I2P;
356 }
else if (c.
id == 4) {
357 c.m_network = NET_ONION;
359 c.m_network = NET_IPV6;
373 if (c.
id == 7 || c.
id == 11) {
374 c.m_network = NET_I2P;
375 }
else if (c.
id == 9 || c.
id == 10) {
376 c.m_network = NET_ONION;
378 c.m_network = NET_IPV4;
381 {0, 1, 2, 3, 6, 7, 9, 11},
382 {4, 5, 8, 10, 12, 13, 14, 15},
392 if (c.
id > 14 && c.
id < 23) {
393 c.m_network = NET_I2P;
394 }
else if (c.
id == 23) {
395 c.m_network = NET_ONION;
397 c.m_network = NET_IPV6;
400 {0, 1, 2, 3, 4, 5, 12, 15, 16, 17, 18, 23},
401 {6, 7, 8, 9, 10, 11, 13, 14, 19, 20, 21, 22},
411 if (c.
id == 12 || c.
id == 14 || c.
id == 17) {
412 c.m_network = NET_I2P;
413 }
else if (c.
id > 17) {
414 c.m_network = NET_ONION;
416 c.m_network = NET_IPV4;
419 {0, 1, 2, 3, 4, 5, 12, 14, 15, 17, 18, 19},
420 {6, 7, 8, 9, 10, 11, 13, 16, 20, 21, 22, 23},
431 c.m_network = NET_I2P;
432 }
else if (c.
id == 12 || c.
id == 14 || c.
id == 15 || c.
id == 16) {
433 c.m_network = NET_ONION;
435 c.m_network = NET_IPV6;
438 {0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 17, 18},
439 {6, 7, 8, 9, 10, 11, 16, 19, 20, 21, 22, 23},
449 if (c.
id > 10 && c.
id < 15) {
450 c.m_network = NET_CJDNS;
451 }
else if (c.
id > 6 && c.
id < 10) {
452 c.m_network = NET_ONION;
454 c.m_network = NET_IPV4;
457 {0, 1, 2, 3, 4, 5, 7, 8, 11, 12, 16, 17},
458 {6, 9, 10, 13, 14, 15, 18, 19, 20, 21, 22, 23},
472 c.m_network = NET_CJDNS;
473 }
else if (c.
id == 1) {
474 c.m_network = NET_I2P;
475 }
else if (c.
id == 2) {
476 c.m_network = NET_ONION;
478 c.m_network = NET_IPV6;
494 c.m_network = NET_CJDNS;
495 }
else if (c.
id == 5) {
496 c.m_network = NET_I2P;
497 }
else if (c.
id == 3) {
498 c.m_network = NET_ONION;
500 c.m_network = NET_IPV4;
516 c.m_network = NET_CJDNS;
517 }
else if (c.
id == 6) {
518 c.m_network = NET_I2P;
519 }
else if (c.
id == 3) {
520 c.m_network = NET_ONION;
522 c.m_network = NET_IPV6;
536 if (c.
id == 11 || c.
id == 15) {
537 c.m_network = NET_CJDNS;
538 }
else if (c.
id == 10 || c.
id == 14) {
539 c.m_network = NET_I2P;
540 }
else if (c.
id == 8 || c.
id == 9) {
541 c.m_network = NET_ONION;
543 c.m_network = NET_IPV4;
546 {0, 1, 2, 3, 6, 8, 10, 11},
547 {4, 5, 7, 9, 12, 13, 14, 15},
558 c.m_network = NET_CJDNS;
559 }
else if (c.
id == 17) {
560 c.m_network = NET_I2P;
561 }
else if (c.
id == 12 || c.
id == 14 || c.
id == 15 || c.
id == 16) {
562 c.m_network = NET_ONION;
564 c.m_network = NET_IPV6;
567 {0, 1, 2, 3, 4, 5, 12, 13, 14, 17, 18, 19},
568 {6, 7, 8, 9, 10, 11, 15, 16, 20, 21, 22, 23},
575 std::shuffle(candidates.begin(), candidates.end(), random_context);
576 const std::optional<NodeId> evicted_node_id =
SelectNodeToEvict(std::move(candidates));
577 if (!evicted_node_id) {
580 return node_ids.count(*evicted_node_id);
590 candidate_setup_fn(candidate);
592 return IsEvicted(candidates, node_ids, random_context);
599 for (
int number_of_nodes = 0; number_of_nodes < 200; ++number_of_nodes) {
606 {0, 1, 2, 3}, random_context));
614 {0, 1, 2, 3, 4, 5, 6, 7}, random_context));
620 candidate.
m_last_tx_time = std::chrono::seconds{number_of_nodes - candidate.
id};
622 {0, 1, 2, 3}, random_context));
629 if (candidate.
id <= 7) {
630 candidate.m_relay_txs = false;
631 candidate.fRelevantServices = true;
634 {0, 1, 2, 3, 4, 5, 6, 7}, random_context));
642 {0, 1, 2, 3}, random_context));
648 if (candidate.
id <= 7) {
649 candidate.m_relay_txs = false;
650 candidate.fRelevantServices = true;
653 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, random_context));
660 candidate.
m_last_tx_time = std::chrono::seconds{number_of_nodes - candidate.
id};
663 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context));
668 if (number_of_nodes >= 29) {
675 if (number_of_nodes <= 20) {
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
void ProtectEvictionCandidatesByRatio(std::vector< NodeEvictionCandidate > &eviction_candidates)
Protect desirable or disadvantaged inbound peers from eviction by ratio.
std::optional< NodeId > SelectNodeToEvict(std::vector< NodeEvictionCandidate > &&vEvictionCandidates)
Select an inbound peer to evict after filtering out (protecting) peers having distinct,...
bool IsEvicted(std::vector< NodeEvictionCandidate > candidates, const std::unordered_set< NodeId > &node_ids, FastRandomContext &random_context)
BOOST_AUTO_TEST_CASE(peer_protection_test)
bool IsProtected(int num_peers, std::function< void(NodeEvictionCandidate &)> candidate_setup_fn, const std::unordered_set< NodeId > &protected_peer_ids, const std::unordered_set< NodeId > &unprotected_peer_ids, FastRandomContext &random_context)
@ NET_ONION
TOR (v2 or v3)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::chrono::seconds m_last_tx_time
std::chrono::seconds m_connected
std::chrono::seconds m_last_block_time
std::chrono::microseconds m_min_ping_time
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)