Skip to content

Commit ce02dd1

Browse files
committed
p2p: extend inbound eviction protection by network to I2P peers
This commit extends our inbound eviction protection to I2P peers to favorise the diversity of peer connections, as peers connected through the I2P network are otherwise disadvantaged by our eviction criteria for their higher latency (higher min ping times) relative to IPv4 and IPv6 peers, as well as relative to Tor onion 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 I2P candidates before localhost and onion ones in terms of opportunity to recover unused remaining protected slots from the previous iteration, guesstimating that most nodes allowing both onion and I2P inbounds will have more onion peers, followed by localhost, then I2P, as I2P support is only being added in the upcoming v22.0 release.
1 parent 70bbc62 commit ce02dd1

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/net.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,16 +908,17 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
908908
// Protect the half of the remaining nodes which have been connected the longest.
909909
// This replicates the non-eviction implicit behavior, and precludes attacks that start later.
910910
// To favorise the diversity of our peer connections, reserve up to half of these protected
911-
// spots for Tor/onion and localhost peers, even if they're not longest uptime overall.
911+
// spots for Tor/onion, localhost and I2P peers, even if they're not longest uptime overall.
912912
// This helps protect these higher-latency peers that tend to be otherwise
913913
// disadvantaged under our eviction criteria.
914914
const size_t initial_size = eviction_candidates.size();
915915
const size_t total_protect_size{initial_size / 2};
916916

917-
// Disadvantaged networks to protect: localhost and Tor/onion. In case of equal counts, earlier
917+
// Disadvantaged networks to protect: I2P, localhost, Tor/onion. In case of equal counts, earlier
918918
// array members have first opportunity to recover unused slots from the previous iteration.
919919
struct Net { bool is_local; Network id; size_t count; };
920-
std::array<Net, 3> networks{{{/* localhost */ true, NET_MAX, 0}, {false, NET_ONION, 0}}};
920+
std::array<Net, 3> networks{
921+
{{false, NET_I2P, 0}, {/* localhost */ true, NET_MAX, 0}, {false, NET_ONION, 0}}};
921922

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

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,8 @@ struct NodeEvictionCandidate
12361236
* - localhost peers, as manually configured hidden services not using
12371237
* `-bind=addr[:port]=onion` will not be detected as inbound onion connections
12381238
*
1239+
* - I2P peers
1240+
*
12391241
* This helps protect these privacy network peers, which tend to be otherwise
12401242
* disadvantaged under our eviction criteria for their higher min ping times
12411243
* relative to IPv4/IPv6 peers, and favorise the diversity of peer connections.

src/test/net_peer_eviction_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
109109
/* unprotected_peer_ids */ {0, 1, 2, 3, 4, 5},
110110
random_context));
111111

112-
// Test protection of onion and localhost peers...
112+
// Test protection of onion, localhost, and I2P peers...
113113

114114
// Expect 1/4 onion peers to be protected from eviction,
115-
// if no localhost peers.
115+
// if no localhost or I2P peers.
116116
BOOST_CHECK(IsProtected(
117117
num_peers, [](NodeEvictionCandidate& c) {
118118
c.m_is_local = false;
@@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
123123
random_context));
124124

125125
// Expect 1/4 onion peers and 1/4 of the other peers to be protected,
126-
// sorted by longest uptime (lowest nTimeConnected), if no localhost peers.
126+
// sorted by longest uptime (lowest nTimeConnected), if no localhost or I2P peers.
127127
BOOST_CHECK(IsProtected(
128128
num_peers, [](NodeEvictionCandidate& c) {
129129
c.nTimeConnected = c.id;
@@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
135135
random_context));
136136

137137
// Expect 1/4 localhost peers to be protected from eviction,
138-
// if no onion peers.
138+
// if no onion or I2P peers.
139139
BOOST_CHECK(IsProtected(
140140
num_peers, [](NodeEvictionCandidate& c) {
141141
c.m_is_local = (c.id == 1 || c.id == 9 || c.id == 11);
@@ -146,7 +146,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
146146
random_context));
147147

148148
// Expect 1/4 localhost peers and 1/4 of the other peers to be protected,
149-
// sorted by longest uptime (lowest nTimeConnected), if no onion peers.
149+
// sorted by longest uptime (lowest nTimeConnected), if no onion or I2P peers.
150150
BOOST_CHECK(IsProtected(
151151
num_peers, [](NodeEvictionCandidate& c) {
152152
c.nTimeConnected = c.id;

0 commit comments

Comments
 (0)