diff --git a/swarm/network/kademlia.go b/swarm/network/kademlia.go index 611159f082..07594053b1 100644 --- a/swarm/network/kademlia.go +++ b/swarm/network/kademlia.go @@ -258,13 +258,11 @@ func (k *Kademlia) SuggestPeer() (a OverlayAddr, o int, want bool) { // try to select a candidate peer // find the first callable peer nxt := bpo[0] - var i int k.addrs.EachBin(k.base, pof, nxt, func(po, _ int, f func(func(pot.Val, int) bool) bool) bool { // for each bin (up until depth) we find callable candidate peers - if po >= depth || po != nxt { + if po >= depth { return false } - i++ ok := f(func(val pot.Val, _ int) bool { a = k.callable(val) return a == nil @@ -272,10 +270,6 @@ func (k *Kademlia) SuggestPeer() (a OverlayAddr, o int, want bool) { if !ok { return false } - if i >= len(bpo) { - return false - } - nxt = bpo[i] return true }) // found a candidate @@ -689,10 +683,10 @@ func (k *Kademlia) full(emptyBins []int) (full bool) { ok := true depth := k.neighbourhoodDepth() k.conns.EachBin(k.base, pof, 0, func(po, _ int, _ func(func(val pot.Val, i int) bool) bool) bool { + if prev == depth+1 { + return true + } for i := prev; i < po; i++ { - if prev == depth+1 { - return true - } e-- if e < 0 { ok = false @@ -703,6 +697,7 @@ func (k *Kademlia) full(emptyBins []int) (full bool) { if emptyBins[e] < i { panic("incorrect peerpot") } + ok = false return false } } diff --git a/swarm/network/kademlia_test.go b/swarm/network/kademlia_test.go index 9d9ddbc934..17feded992 100644 --- a/swarm/network/kademlia_test.go +++ b/swarm/network/kademlia_test.go @@ -228,19 +228,18 @@ func TestSuggestPeerFindPeers(t *testing.T) { } k.Register("01000001") - err = testSuggestPeer(t, k, "", 0, false) - if err != nil { - t.Fatal(err.Error()) - } - - k.On("10000001") - log.Trace("Kad:\n%v", k.String()) err = testSuggestPeer(t, k, "01000001", 0, false) if err != nil { t.Fatal(err.Error()) } k.On("10000001") + log.Trace(fmt.Sprintf("Kad:\n%v", k.String())) + err = testSuggestPeer(t, k, "", 1, true) + if err != nil { + t.Fatal(err.Error()) + } + k.On("01000001") err = testSuggestPeer(t, k, "", 0, false) if err != nil { @@ -283,7 +282,7 @@ func TestSuggestPeerFindPeers(t *testing.T) { func TestSuggestPeerRetries(t *testing.T) { // 2 row gap, unsaturated proxbin, no callables -> want PO 0 k := newTestKademlia("00000000") - k.RetryInterval = int64(time.Second) // cycle + k.RetryInterval = int64(100 * time.Millisecond) // cycle k.MaxRetries = 50 k.RetryExponent = 2 sleep := func(n int) {