Skip to content

Commit 0cca08a

Browse files
committed
Add unit test coverage for our onion peer eviction protection
1 parent caa21f5 commit 0cca08a

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

src/test/net_peer_eviction_tests.cpp

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,29 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
110110
/* unprotected_peer_ids */ {0, 1, 2, 3, 4, 5},
111111
random_context));
112112

113-
// Test protection of localhost peers...
113+
// Test protection of onion and localhost peers...
114+
115+
// Expect 1/4 onion peers to be protected from eviction,
116+
// independently of other characteristics.
117+
BOOST_CHECK(IsProtected(
118+
num_peers, [](NodeEvictionCandidate& c) {
119+
c.m_is_onion = (c.id == 3 || c.id == 8 || c.id == 9);
120+
},
121+
/* protected_peer_ids */ {3, 8, 9},
122+
/* unprotected_peer_ids */ {},
123+
random_context));
124+
125+
// Expect 1/4 onion peers and 1/4 of the others to be protected
126+
// from eviction, sorted by longest uptime (lowest nTimeConnected).
127+
BOOST_CHECK(IsProtected(
128+
num_peers, [](NodeEvictionCandidate& c) {
129+
c.nTimeConnected = c.id;
130+
c.m_is_local = false;
131+
c.m_is_onion = (c.id == 3 || c.id > 7);
132+
},
133+
/* protected_peer_ids */ {0, 1, 2, 3, 8, 9},
134+
/* unprotected_peer_ids */ {4, 5, 6, 7, 10, 11},
135+
random_context));
114136

115137
// Expect 1/4 localhost peers to be protected from eviction,
116138
// if no onion peers.
@@ -134,6 +156,78 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
134156
/* protected_peer_ids */ {0, 1, 2, 7, 8, 9},
135157
/* unprotected_peer_ids */ {3, 4, 5, 6, 10, 11},
136158
random_context));
159+
160+
// Combined test: expect 1/4 onion and 2 localhost peers to be protected
161+
// from eviction, sorted by longest uptime.
162+
BOOST_CHECK(IsProtected(
163+
num_peers, [](NodeEvictionCandidate& c) {
164+
c.nTimeConnected = c.id;
165+
c.m_is_onion = (c.id == 0 || c.id == 5 || c.id == 10);
166+
c.m_is_local = (c.id == 1 || c.id == 9 || c.id == 11);
167+
},
168+
/* protected_peer_ids */ {0, 1, 2, 5, 9, 10},
169+
/* unprotected_peer_ids */ {3, 4, 6, 7, 8, 11},
170+
random_context));
171+
172+
// Combined test: expect having only 1 onion to allow allocating the
173+
// remaining 2 of the 1/4 to localhost peers, sorted by longest uptime.
174+
BOOST_CHECK(IsProtected(
175+
num_peers + 4, [](NodeEvictionCandidate& c) {
176+
c.nTimeConnected = c.id;
177+
c.m_is_onion = (c.id == 15);
178+
c.m_is_local = (c.id > 6 && c.id < 11);
179+
},
180+
/* protected_peer_ids */ {0, 1, 2, 3, 7, 8, 9, 15},
181+
/* unprotected_peer_ids */ {4, 5, 6, 10, 11, 12, 13, 14},
182+
random_context));
183+
184+
// Combined test: expect 2 onions (< 1/4) to allow allocating the minimum 2
185+
// localhost peers, sorted by longest uptime.
186+
BOOST_CHECK(IsProtected(
187+
num_peers, [](NodeEvictionCandidate& c) {
188+
c.nTimeConnected = c.id;
189+
c.m_is_onion = (c.id == 7 || c.id == 9);
190+
c.m_is_local = (c.id == 6 || c.id == 11);
191+
},
192+
/* protected_peer_ids */ {0, 1, 6, 7, 9, 11},
193+
/* unprotected_peer_ids */ {2, 3, 4, 5, 8, 10},
194+
random_context));
195+
196+
// Combined test: when > 1/4, expect max 1/4 onion and 2 localhost peers
197+
// to be protected from eviction, sorted by longest uptime.
198+
BOOST_CHECK(IsProtected(
199+
num_peers, [](NodeEvictionCandidate& c) {
200+
c.nTimeConnected = c.id;
201+
c.m_is_onion = (c.id > 3 && c.id < 8);
202+
c.m_is_local = (c.id > 7);
203+
},
204+
/* protected_peer_ids */ {0, 4, 5, 6, 8, 9},
205+
/* unprotected_peer_ids */ {1, 2, 3, 7, 10, 11},
206+
random_context));
207+
208+
// Combined test: idem > 1/4 with only 8 peers: expect 2 onion and 2
209+
// localhost peers (1/4 + 2) to be protected, sorted by longest uptime.
210+
BOOST_CHECK(IsProtected(
211+
8, [](NodeEvictionCandidate& c) {
212+
c.nTimeConnected = c.id;
213+
c.m_is_onion = (c.id > 1 && c.id < 5);
214+
c.m_is_local = (c.id > 4);
215+
},
216+
/* protected_peer_ids */ {2, 3, 5, 6},
217+
/* unprotected_peer_ids */ {0, 1, 4, 7},
218+
random_context));
219+
220+
// Combined test: idem > 1/4 with only 6 peers: expect 1 onion peer and no
221+
// localhost peers (1/4 + 0) to be protected, sorted by longest uptime.
222+
BOOST_CHECK(IsProtected(
223+
6, [](NodeEvictionCandidate& c) {
224+
c.nTimeConnected = c.id;
225+
c.m_is_onion = (c.id == 4 || c.id == 5);
226+
c.m_is_local = (c.id == 3);
227+
},
228+
/* protected_peer_ids */ {0, 1, 4},
229+
/* unprotected_peer_ids */ {2, 3, 5},
230+
random_context));
137231
}
138232

139233
// Returns true if any of the node ids in node_ids are selected for eviction.

0 commit comments

Comments
 (0)