Commit 72dd3718 authored by Brian Tiger Chow's avatar Brian Tiger Chow

Merge pull request #425 from jbenet/fix/bootstrap-panic

fix(core) bootstrap panic
parents 5f3ae91f f8a44995
......@@ -11,6 +11,7 @@ import (
inet "github.com/jbenet/go-ipfs/net"
peer "github.com/jbenet/go-ipfs/peer"
dht "github.com/jbenet/go-ipfs/routing/dht"
math2 "github.com/jbenet/go-ipfs/util/math2"
)
const (
......@@ -68,10 +69,7 @@ func bootstrap(ctx context.Context,
}
}
var randomSubset []peer.Peer
for _, val := range rand.Perm(numCxnsToCreate) {
randomSubset = append(randomSubset, notConnected[val])
}
var randomSubset = randomSubsetOfPeers(notConnected, numCxnsToCreate)
if err := connect(ctx, r, randomSubset); err != nil {
return err
}
......@@ -119,3 +117,12 @@ func toPeer(ps peer.Peerstore, bootstrap *config.BootstrapPeer) (peer.Peer, erro
p.AddAddress(maddr)
return p, nil
}
func randomSubsetOfPeers(in []peer.Peer, max int) []peer.Peer {
n := math2.IntMin(max, len(in))
var out []peer.Peer
for _, val := range rand.Perm(n) {
out = append(out, in[val])
}
return out
}
package core
import (
"testing"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestSubsetWhenMaxIsGreaterThanLengthOfSlice(t *testing.T) {
var ps []peer.Peer
sizeofSlice := 100
for i := 0; i < sizeofSlice; i++ {
ps = append(ps, testutil.RandPeer())
}
out := randomSubsetOfPeers(ps, 2*sizeofSlice)
if len(out) != len(ps) {
t.Fail()
}
}
package math2
// IntMin returns the smaller of x or y.
func IntMin(x, y int) int {
if x < y {
return x
}
return y
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment