Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combine multiple bootstrap addrs into single peer info #2199

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,22 @@ func bootstrapConnect(ctx context.Context, ph host.Host, peers []peer.PeerInfo)
}

func toPeerInfos(bpeers []config.BootstrapPeer) []peer.PeerInfo {
var peers []peer.PeerInfo
pinfos := make(map[peer.ID]*peer.PeerInfo)
for _, bootstrap := range bpeers {
peers = append(peers, toPeerInfo(bootstrap))
pinfo, ok := pinfos[bootstrap.ID()]
if !ok {
pinfo = new(peer.PeerInfo)
pinfos[bootstrap.ID()] = pinfo
pinfo.ID = bootstrap.ID()
}
pinfo.Addrs = append(pinfo.Addrs, bootstrap.Multiaddr())
}

var peers []peer.PeerInfo
for _, pinfo := range pinfos {
peers = append(peers, *pinfo)
}

return peers
}

Expand Down
31 changes: 31 additions & 0 deletions core/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package core

import (
"fmt"
"testing"

peer "github.com/ipfs/go-ipfs/p2p/peer"
config "github.com/ipfs/go-ipfs/repo/config"
testutil "github.com/ipfs/go-ipfs/util/testutil"
)

Expand All @@ -23,3 +25,32 @@ func TestSubsetWhenMaxIsGreaterThanLengthOfSlice(t *testing.T) {
t.Fail()
}
}

func TestMultipleAddrsPerPeer(t *testing.T) {
var bsps []config.BootstrapPeer
for i := 0; i < 10; i++ {
pid, err := testutil.RandPeerID()
if err != nil {
t.Fatal(err)
}

addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/5001/ipfs/%s", pid.Pretty())
bsp1, err := config.ParseBootstrapPeer(addr)
if err != nil {
t.Fatal(err)
}

addr = fmt.Sprintf("/ip4/127.0.0.1/udp/5002/utp/ipfs/%s", pid.Pretty())
bsp2, err := config.ParseBootstrapPeer(addr)
if err != nil {
t.Fatal(err)
}

bsps = append(bsps, bsp1, bsp2)
}

pinfos := toPeerInfos(bsps)
if len(pinfos) != len(bsps)/2 {
t.Fatal("expected fewer peers")
}
}