@@ -168,6 +168,46 @@ func TestNeighbourhoodDepth(t *testing.T) {
168
168
testNum ++
169
169
}
170
170
171
+ // TestHighMinBinSize tests that the saturation function also works
172
+ // if MinBinSize is > 2, the connection count is < k.MinBinSize
173
+ // and there are more peers available than connected
174
+ func TestHighMinBinSize (t * testing.T ) {
175
+ // a function to test for different MinBinSize values
176
+ testKad := func (minBinSize int ) {
177
+ // create a test kademlia
178
+ tk := newTestKademlia (t , "11111111" )
179
+ // set its MinBinSize to desired value
180
+ tk .KadParams .MinBinSize = minBinSize
181
+
182
+ // add a couple of peers (so we have NN and depth)
183
+ tk .On ("00000000" ) // bin 0
184
+ tk .On ("11100000" ) // bin 3
185
+ tk .On ("11110000" ) // bin 4
186
+
187
+ first := "10000000" // add a first peer at bin 1
188
+ tk .Register (first ) // register it
189
+ // we now have one registered peer at bin 1;
190
+ // iterate and connect one peer at each iteration;
191
+ // should be unhealthy until at minBinSize - 1
192
+ // we connect the unconnected but registered peer
193
+ for i := 1 ; i < minBinSize ; i ++ {
194
+ peer := fmt .Sprintf ("1000%b" , 8 | i )
195
+ tk .On (peer )
196
+ if i == minBinSize - 1 {
197
+ tk .On (first )
198
+ tk .checkHealth (true )
199
+ return
200
+ }
201
+ tk .checkHealth (false )
202
+ }
203
+ }
204
+ // test MinBinSizes of 3 to 5
205
+ testMinBinSizes := []int {3 , 4 , 5 }
206
+ for _ , k := range testMinBinSizes {
207
+ testKad (k )
208
+ }
209
+ }
210
+
171
211
// TestHealthStrict tests the simplest definition of health
172
212
// Which means whether we are connected to all neighbors we know of
173
213
func TestHealthStrict (t * testing.T ) {
@@ -176,60 +216,116 @@ func TestHealthStrict(t *testing.T) {
176
216
// no peers
177
217
// unhealthy (and lonely)
178
218
tk := newTestKademlia (t , "11111111" )
179
- tk .checkHealth (false , false )
219
+ tk .checkHealth (false )
180
220
181
221
// know one peer but not connected
182
222
// unhealthy
183
223
tk .Register ("11100000" )
184
- tk .checkHealth (false , false )
224
+ tk .checkHealth (false )
185
225
186
226
// know one peer and connected
187
- // healthy
227
+ // unhealthy: not saturated
188
228
tk .On ("11100000" )
189
- tk .checkHealth (true , false )
229
+ tk .checkHealth (true )
190
230
191
231
// know two peers, only one connected
192
232
// unhealthy
193
233
tk .Register ("11111100" )
194
- tk .checkHealth (false , false )
234
+ tk .checkHealth (false )
195
235
196
236
// know two peers and connected to both
197
237
// healthy
198
238
tk .On ("11111100" )
199
- tk .checkHealth (true , false )
239
+ tk .checkHealth (true )
200
240
201
241
// know three peers, connected to the two deepest
202
242
// healthy
203
243
tk .Register ("00000000" )
204
- tk .checkHealth (true , false )
244
+ tk .checkHealth (false )
205
245
206
246
// know three peers, connected to all three
207
247
// healthy
208
248
tk .On ("00000000" )
209
- tk .checkHealth (true , false )
249
+ tk .checkHealth (true )
210
250
211
251
// add fourth peer deeper than current depth
212
252
// unhealthy
213
253
tk .Register ("11110000" )
214
- tk .checkHealth (false , false )
254
+ tk .checkHealth (false )
215
255
216
256
// connected to three deepest peers
217
257
// healthy
218
258
tk .On ("11110000" )
219
- tk .checkHealth (true , false )
259
+ tk .checkHealth (true )
220
260
221
261
// add additional peer in same bin as deepest peer
222
262
// unhealthy
223
263
tk .Register ("11111101" )
224
- tk .checkHealth (false , false )
264
+ tk .checkHealth (false )
225
265
226
266
// four deepest of five peers connected
227
267
// healthy
228
268
tk .On ("11111101" )
229
- tk .checkHealth (true , false )
269
+ tk .checkHealth (true )
270
+
271
+ // add additional peer in bin 0
272
+ // unhealthy: unsaturated bin 0, 2 known but 1 connected
273
+ tk .Register ("00000001" )
274
+ tk .checkHealth (false )
275
+
276
+ // Connect second in bin 0
277
+ // healthy
278
+ tk .On ("00000001" )
279
+ tk .checkHealth (true )
280
+
281
+ // add peer in bin 1
282
+ // unhealthy, as it is known but not connected
283
+ tk .Register ("10000000" )
284
+ tk .checkHealth (false )
285
+
286
+ // connect peer in bin 1
287
+ // depth change, is now 1
288
+ // healthy, 1 peer in bin 1 known and connected
289
+ tk .On ("10000000" )
290
+ tk .checkHealth (true )
291
+
292
+ // add second peer in bin 1
293
+ // unhealthy, as it is known but not connected
294
+ tk .Register ("10000001" )
295
+ tk .checkHealth (false )
296
+
297
+ // connect second peer in bin 1
298
+ // healthy,
299
+ tk .On ("10000001" )
300
+ tk .checkHealth (true )
301
+
302
+ // connect third peer in bin 1
303
+ // healthy,
304
+ tk .On ("10000011" )
305
+ tk .checkHealth (true )
306
+
307
+ // add peer in bin 2
308
+ // unhealthy, no depth change
309
+ tk .Register ("11000000" )
310
+ tk .checkHealth (false )
311
+
312
+ // connect peer in bin 2
313
+ // depth change - as we already have peers in bin 3 and 4,
314
+ // we have contiguous bins, no bin < po 5 is empty -> depth 5
315
+ // healthy, every bin < depth has the max available peers,
316
+ // even if they are < MinBinSize
317
+ tk .On ("11000000" )
318
+ tk .checkHealth (true )
319
+
320
+ // add peer in bin 2
321
+ // unhealthy, peer bin is below depth 5 but
322
+ // has more available peers (2) than connected ones (1)
323
+ // --> unsaturated
324
+ tk .Register ("11000011" )
325
+ tk .checkHealth (false )
230
326
}
231
327
232
- func (tk * testKademlia ) checkHealth (expectHealthy bool , expectSaturation bool ) {
328
+ func (tk * testKademlia ) checkHealth (expectHealthy bool ) {
233
329
tk .t .Helper ()
234
330
kid := common .Bytes2Hex (tk .BaseAddr ())
235
331
addrs := [][]byte {tk .BaseAddr ()}
@@ -239,13 +335,13 @@ func (tk *testKademlia) checkHealth(expectHealthy bool, expectSaturation bool) {
239
335
})
240
336
241
337
pp := NewPeerPotMap (tk .NeighbourhoodSize , addrs )
242
- healthParams := tk .Healthy (pp [kid ])
338
+ healthParams := tk .GetHealthInfo (pp [kid ])
243
339
244
340
// definition of health, all conditions but be true:
245
341
// - we at least know one peer
246
342
// - we know all neighbors
247
343
// - we are connected to all known neighbors
248
- health := healthParams .KnowNN && healthParams . ConnectNN && healthParams . CountKnowNN > 0
344
+ health := healthParams .Healthy ()
249
345
if expectHealthy != health {
250
346
tk .t .Fatalf ("expected kademlia health %v, is %v\n %v" , expectHealthy , health , tk .String ())
251
347
}
0 commit comments