Commit 1649b44a authored by Jeromy's avatar Jeromy

combine multiple bootstrap addrs into single peer info

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 3c6a40a3
......@@ -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
}
......
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"
)
......@@ -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")
}
}
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