@@ -156,6 +156,110 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
156
156
/* protected_peer_ids */ {0 , 1 , 2 , 7 , 8 , 9 },
157
157
/* unprotected_peer_ids */ {3 , 4 , 5 , 6 , 10 , 11 },
158
158
random_context));
159
+
160
+ // Tests with 2 networks...
161
+
162
+ // Combined test: expect having 1 localhost and 1 onion peer out of 4 to
163
+ // protect 1 localhost, 0 onion and 1 other peer, sorted by longest uptime;
164
+ // stable sort breaks tie with array order of localhost first.
165
+ BOOST_CHECK (IsProtected (
166
+ 4 , [](NodeEvictionCandidate& c) {
167
+ c.nTimeConnected = c.id ;
168
+ c.m_is_local = (c.id == 4 );
169
+ c.m_network = (c.id == 3 ) ? NET_ONION : NET_IPV4;
170
+ },
171
+ /* protected_peer_ids */ {0 , 4 },
172
+ /* unprotected_peer_ids */ {1 , 2 },
173
+ random_context));
174
+
175
+ // Combined test: expect having 1 localhost and 1 onion peer out of 7 to
176
+ // protect 1 localhost, 0 onion, and 2 other peers (3 total), sorted by
177
+ // uptime; stable sort breaks tie with array order of localhost first.
178
+ BOOST_CHECK (IsProtected (
179
+ 7 , [](NodeEvictionCandidate& c) {
180
+ c.nTimeConnected = c.id ;
181
+ c.m_is_local = (c.id == 6 );
182
+ c.m_network = (c.id == 5 ) ? NET_ONION : NET_IPV4;
183
+ },
184
+ /* protected_peer_ids */ {0 , 1 , 6 },
185
+ /* unprotected_peer_ids */ {2 , 3 , 4 , 5 },
186
+ random_context));
187
+
188
+ // Combined test: expect having 1 localhost and 1 onion peer out of 8 to
189
+ // protect protect 1 localhost, 1 onion and 2 other peers (4 total), sorted
190
+ // by uptime; stable sort breaks tie with array order of localhost first.
191
+ BOOST_CHECK (IsProtected (
192
+ 8 , [](NodeEvictionCandidate& c) {
193
+ c.nTimeConnected = c.id ;
194
+ c.m_is_local = (c.id == 6 );
195
+ c.m_network = (c.id == 5 ) ? NET_ONION : NET_IPV4;
196
+ },
197
+ /* protected_peer_ids */ {0 , 1 , 5 , 6 },
198
+ /* unprotected_peer_ids */ {2 , 3 , 4 , 7 },
199
+ random_context));
200
+
201
+ // Combined test: expect having 3 localhost and 3 onion peers out of 12 to
202
+ // protect 2 localhost and 1 onion, plus 3 other peers, sorted by longest
203
+ // uptime; stable sort breaks ties with the array order of localhost first.
204
+ BOOST_CHECK (IsProtected (
205
+ num_peers, [](NodeEvictionCandidate& c) {
206
+ c.nTimeConnected = c.id ;
207
+ c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11 );
208
+ c.m_network = (c.id == 7 || c.id == 8 || c.id == 10 ) ? NET_ONION : NET_IPV6;
209
+ },
210
+ /* protected_peer_ids */ {0 , 1 , 2 , 6 , 7 , 9 },
211
+ /* unprotected_peer_ids */ {3 , 4 , 5 , 8 , 10 , 11 },
212
+ random_context));
213
+
214
+ // Combined test: expect having 4 localhost and 1 onion peer out of 12 to
215
+ // protect 2 localhost and 1 onion, plus 3 other peers, sorted by longest uptime.
216
+ BOOST_CHECK (IsProtected (
217
+ num_peers, [](NodeEvictionCandidate& c) {
218
+ c.nTimeConnected = c.id ;
219
+ c.m_is_local = (c.id > 4 && c.id < 9 );
220
+ c.m_network = (c.id == 10 ) ? NET_ONION : NET_IPV4;
221
+ },
222
+ /* protected_peer_ids */ {0 , 1 , 2 , 5 , 6 , 10 },
223
+ /* unprotected_peer_ids */ {3 , 4 , 7 , 8 , 9 , 11 },
224
+ random_context));
225
+
226
+ // Combined test: expect having 4 localhost and 2 onion peers out of 16 to
227
+ // protect 2 localhost and 2 onions, plus 4 other peers, sorted by longest uptime.
228
+ BOOST_CHECK (IsProtected (
229
+ 16 , [](NodeEvictionCandidate& c) {
230
+ c.nTimeConnected = c.id ;
231
+ c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11 || c.id == 12 );
232
+ c.m_network = (c.id == 8 || c.id == 10 ) ? NET_ONION : NET_IPV6;
233
+ },
234
+ /* protected_peer_ids */ {0 , 1 , 2 , 3 , 6 , 8 , 9 , 10 },
235
+ /* unprotected_peer_ids */ {4 , 5 , 7 , 11 , 12 , 13 , 14 , 15 },
236
+ random_context));
237
+
238
+ // Combined test: expect having 5 localhost and 1 onion peer out of 16 to
239
+ // protect 3 localhost (recovering the unused onion slot), 1 onion, and 4
240
+ // others, sorted by longest uptime.
241
+ BOOST_CHECK (IsProtected (
242
+ 16 , [](NodeEvictionCandidate& c) {
243
+ c.nTimeConnected = c.id ;
244
+ c.m_is_local = (c.id > 10 );
245
+ c.m_network = (c.id == 10 ) ? NET_ONION : NET_IPV4;
246
+ },
247
+ /* protected_peer_ids */ {0 , 1 , 2 , 3 , 10 , 11 , 12 , 13 },
248
+ /* unprotected_peer_ids */ {4 , 5 , 6 , 7 , 8 , 9 , 14 , 15 },
249
+ random_context));
250
+
251
+ // Combined test: expect having 1 localhost and 4 onion peers out of 16 to
252
+ // protect 1 localhost and 3 onions (recovering the unused localhost slot),
253
+ // plus 4 others, sorted by longest uptime.
254
+ BOOST_CHECK (IsProtected (
255
+ 16 , [](NodeEvictionCandidate& c) {
256
+ c.nTimeConnected = c.id ;
257
+ c.m_is_local = (c.id == 15 );
258
+ c.m_network = (c.id > 6 && c.id < 11 ) ? NET_ONION : NET_IPV6;
259
+ },
260
+ /* protected_peer_ids */ {0 , 1 , 2 , 3 , 7 , 8 , 9 , 15 },
261
+ /* unprotected_peer_ids */ {5 , 6 , 10 , 11 , 12 , 13 , 14 },
262
+ random_context));
159
263
}
160
264
161
265
// Returns true if any of the node ids in node_ids are selected for eviction.
0 commit comments