@@ -168,6 +168,46 @@ func TestNeighbourhoodDepth(t *testing.T) {
168168 testNum ++
169169}
170170
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+
171211// TestHealthStrict tests the simplest definition of health
172212// Which means whether we are connected to all neighbors we know of
173213func TestHealthStrict (t * testing.T ) {
@@ -176,60 +216,116 @@ func TestHealthStrict(t *testing.T) {
176216 // no peers
177217 // unhealthy (and lonely)
178218 tk := newTestKademlia (t , "11111111" )
179- tk .checkHealth (false , false )
219+ tk .checkHealth (false )
180220
181221 // know one peer but not connected
182222 // unhealthy
183223 tk .Register ("11100000" )
184- tk .checkHealth (false , false )
224+ tk .checkHealth (false )
185225
186226 // know one peer and connected
187- // healthy
227+ // unhealthy: not saturated
188228 tk .On ("11100000" )
189- tk .checkHealth (true , false )
229+ tk .checkHealth (true )
190230
191231 // know two peers, only one connected
192232 // unhealthy
193233 tk .Register ("11111100" )
194- tk .checkHealth (false , false )
234+ tk .checkHealth (false )
195235
196236 // know two peers and connected to both
197237 // healthy
198238 tk .On ("11111100" )
199- tk .checkHealth (true , false )
239+ tk .checkHealth (true )
200240
201241 // know three peers, connected to the two deepest
202242 // healthy
203243 tk .Register ("00000000" )
204- tk .checkHealth (true , false )
244+ tk .checkHealth (false )
205245
206246 // know three peers, connected to all three
207247 // healthy
208248 tk .On ("00000000" )
209- tk .checkHealth (true , false )
249+ tk .checkHealth (true )
210250
211251 // add fourth peer deeper than current depth
212252 // unhealthy
213253 tk .Register ("11110000" )
214- tk .checkHealth (false , false )
254+ tk .checkHealth (false )
215255
216256 // connected to three deepest peers
217257 // healthy
218258 tk .On ("11110000" )
219- tk .checkHealth (true , false )
259+ tk .checkHealth (true )
220260
221261 // add additional peer in same bin as deepest peer
222262 // unhealthy
223263 tk .Register ("11111101" )
224- tk .checkHealth (false , false )
264+ tk .checkHealth (false )
225265
226266 // four deepest of five peers connected
227267 // healthy
228268 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 )
230326}
231327
232- func (tk * testKademlia ) checkHealth (expectHealthy bool , expectSaturation bool ) {
328+ func (tk * testKademlia ) checkHealth (expectHealthy bool ) {
233329 tk .t .Helper ()
234330 kid := common .Bytes2Hex (tk .BaseAddr ())
235331 addrs := [][]byte {tk .BaseAddr ()}
@@ -239,13 +335,13 @@ func (tk *testKademlia) checkHealth(expectHealthy bool, expectSaturation bool) {
239335 })
240336
241337 pp := NewPeerPotMap (tk .NeighbourhoodSize , addrs )
242- healthParams := tk .Healthy (pp [kid ])
338+ healthParams := tk .GetHealthInfo (pp [kid ])
243339
244340 // definition of health, all conditions but be true:
245341 // - we at least know one peer
246342 // - we know all neighbors
247343 // - we are connected to all known neighbors
248- health := healthParams .KnowNN && healthParams . ConnectNN && healthParams . CountKnowNN > 0
344+ health := healthParams .Healthy ()
249345 if expectHealthy != health {
250346 tk .t .Fatalf ("expected kademlia health %v, is %v\n %v" , expectHealthy , health , tk .String ())
251347 }
0 commit comments