Skip to content

Commit f7b8094

Browse files
committed
p2p: extend inbound eviction protection by network to CJDNS peers
This commit extends our inbound eviction protection to CJDNS peers to favorise the diversity of peer connections, as peers connected through the CJDNS network are otherwise disadvantaged by our eviction criteria for their higher latency (higher min ping times) relative to IPv4 and IPv6 peers. The `networks` array is order-dependent in the case of a tie in candidate counts between networks; earlier array members receive priority in the case of a tie. Therefore, we place CJDNS candidates before I2P, localhost, and onion ones in terms of opportunity to recover unused remaining protected slots from the previous iteration, estimating that most nodes allowing several inbound privacy networks will have more onion, localhost or I2P peers than CJDNS ones, as CJDNS support is only being added in the upcoming v23.0 release.
1 parent dd405ad commit f7b8094

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/net.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,17 +918,17 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
918918
// Protect the half of the remaining nodes which have been connected the longest.
919919
// This replicates the non-eviction implicit behavior, and precludes attacks that start later.
920920
// To favorise the diversity of our peer connections, reserve up to half of these protected
921-
// spots for Tor/onion, localhost and I2P peers, even if they're not longest uptime overall.
922-
// This helps protect these higher-latency peers that tend to be otherwise
921+
// spots for Tor/onion, localhost, I2P, and CJDNS peers, even if they're not longest uptime
922+
// overall. This helps protect these higher-latency peers that tend to be otherwise
923923
// disadvantaged under our eviction criteria.
924924
const size_t initial_size = eviction_candidates.size();
925925
const size_t total_protect_size{initial_size / 2};
926926

927-
// Disadvantaged networks to protect: I2P, localhost, Tor/onion. In case of equal counts, earlier
928-
// array members have first opportunity to recover unused slots from the previous iteration.
927+
// Disadvantaged networks to protect. In the case of equal counts, earlier array members
928+
// have the first opportunity to recover unused slots from the previous iteration.
929929
struct Net { bool is_local; Network id; size_t count; };
930-
std::array<Net, 3> networks{
931-
{{false, NET_I2P, 0}, {/* localhost */ true, NET_MAX, 0}, {false, NET_ONION, 0}}};
930+
std::array<Net, 4> networks{
931+
{{false, NET_CJDNS, 0}, {false, NET_I2P, 0}, {/*localhost=*/true, NET_MAX, 0}, {false, NET_ONION, 0}}};
932932

933933
// Count and store the number of eviction candidates per network.
934934
for (Net& n : networks) {

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,8 @@ struct NodeEvictionCandidate
13071307
*
13081308
* - I2P peers
13091309
*
1310+
* - CJDNS peers
1311+
*
13101312
* This helps protect these privacy network peers, which tend to be otherwise
13111313
* disadvantaged under our eviction criteria for their higher min ping times
13121314
* relative to IPv4/IPv6 peers, and favorise the diversity of peer connections.

src/test/net_peer_eviction_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
9090
// Test protection of onion, localhost, and I2P peers...
9191

9292
// Expect 1/4 onion peers to be protected from eviction,
93-
// if no localhost or I2P peers.
93+
// if no localhost, I2P, or CJDNS peers.
9494
BOOST_CHECK(IsProtected(
9595
num_peers, [](NodeEvictionCandidate& c) {
9696
c.m_is_local = false;
@@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
101101
random_context));
102102

103103
// Expect 1/4 onion peers and 1/4 of the other peers to be protected,
104-
// sorted by longest uptime (lowest m_connected), if no localhost or I2P peers.
104+
// sorted by longest uptime (lowest m_connected), if no localhost, I2P or CJDNS peers.
105105
BOOST_CHECK(IsProtected(
106106
num_peers, [](NodeEvictionCandidate& c) {
107107
c.m_connected = std::chrono::seconds{c.id};
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
113113
random_context));
114114

115115
// Expect 1/4 localhost peers to be protected from eviction,
116-
// if no onion or I2P peers.
116+
// if no onion, I2P, or CJDNS peers.
117117
BOOST_CHECK(IsProtected(
118118
num_peers, [](NodeEvictionCandidate& c) {
119119
c.m_is_local = (c.id == 1 || c.id == 9 || c.id == 11);
@@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
124124
random_context));
125125

126126
// Expect 1/4 localhost peers and 1/4 of the other peers to be protected,
127-
// sorted by longest uptime (lowest m_connected), if no onion or I2P peers.
127+
// sorted by longest uptime (lowest m_connected), if no onion, I2P, or CJDNS peers.
128128
BOOST_CHECK(IsProtected(
129129
num_peers, [](NodeEvictionCandidate& c) {
130130
c.m_connected = std::chrono::seconds{c.id};
@@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
136136
random_context));
137137

138138
// Expect 1/4 I2P peers to be protected from eviction,
139-
// if no onion or localhost peers.
139+
// if no onion, localhost, or CJDNS peers.
140140
BOOST_CHECK(IsProtected(
141141
num_peers, [](NodeEvictionCandidate& c) {
142142
c.m_is_local = false;
@@ -146,8 +146,8 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
146146
/*unprotected_peer_ids=*/{},
147147
random_context));
148148

149-
// Expect 1/4 I2P peers and 1/4 of the other peers to be protected,
150-
// sorted by longest uptime (lowest m_connected), if no onion or localhost peers.
149+
// Expect 1/4 I2P peers and 1/4 of the other peers to be protected, sorted
150+
// by longest uptime (lowest m_connected), if no onion, localhost, or CJDNS peers.
151151
BOOST_CHECK(IsProtected(
152152
num_peers, [](NodeEvictionCandidate& c) {
153153
c.m_connected = std::chrono::seconds{c.id};

0 commit comments

Comments
 (0)