Skip to content

Commit

Permalink
Merge pull request #6384 from ipfs/libp2p-core
Browse files Browse the repository at this point in the history
migrate to go-libp2p-core.
  • Loading branch information
Stebalien committed Jun 3, 2019
2 parents 07aa4fb + ce5084b commit 1a32379
Show file tree
Hide file tree
Showing 81 changed files with 732 additions and 723 deletions.
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ aliases:
restore_gomod: &restore_gomod
restore_cache:
keys:
- v3-dep-{{ .Branch }}-{{ checksum "~/ipfs/go-ipfs/go.sum" }}-{{ .Environment.CIRCLE_JOB }}
- v3-dep-{{ .Branch }}-{{ checksum "~/ipfs/go-ipfs/go.sum" }}-
- v3-dep-{{ .Branch }}-
- v3-dep-master-
- v4-dep-{{ .Branch }}-{{ checksum "~/ipfs/go-ipfs/go.sum" }}-{{ .Environment.CIRCLE_JOB }}
- v4-dep-{{ .Branch }}-{{ checksum "~/ipfs/go-ipfs/go.sum" }}-
- v4-dep-{{ .Branch }}-
- v4-dep-master-
store_gomod: &store_gomod
save_cache:
key: v3-dep-{{ .Branch }}-{{ checksum "~/ipfs/go-ipfs/go.sum" }}-{{ .Environment.CIRCLE_JOB }}
key: v4-dep-{{ .Branch }}-{{ checksum "~/ipfs/go-ipfs/go.sum" }}-{{ .Environment.CIRCLE_JOB }}
paths:
- ~/go/pkg/mod
- ~/.cache/go-build/
Expand Down
6 changes: 3 additions & 3 deletions cmd/seccat/seccat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"syscall"

logging "github.com/ipfs/go-log"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
ci "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
secio "github.com/libp2p/go-libp2p-secio"
)
Expand Down
54 changes: 15 additions & 39 deletions core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
"sync"
"time"

config "github.com/ipfs/go-ipfs-config"
logging "github.com/ipfs/go-log"
"github.com/jbenet/goprocess"
"github.com/jbenet/goprocess/context"
"github.com/jbenet/goprocess/periodic"
"github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-loggables"
"github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-routing"
)

var log = logging.Logger("bootstrap")
Expand Down Expand Up @@ -51,7 +50,7 @@ type BootstrapConfig struct {
// BootstrapPeers is a function that returns a set of bootstrap peers
// for the bootstrap process to use. This makes it possible for clients
// to control the peers the process uses at any moment.
BootstrapPeers func() []peerstore.PeerInfo
BootstrapPeers func() []peer.AddrInfo
}

// DefaultBootstrapConfig specifies default sane parameters for bootstrapping.
Expand All @@ -61,9 +60,9 @@ var DefaultBootstrapConfig = BootstrapConfig{
ConnectionTimeout: (30 * time.Second) / 3, // Perod / 3
}

func BootstrapConfigWithPeers(pis []peerstore.PeerInfo) BootstrapConfig {
func BootstrapConfigWithPeers(pis []peer.AddrInfo) BootstrapConfig {
cfg := DefaultBootstrapConfig
cfg.BootstrapPeers = func() []peerstore.PeerInfo {
cfg.BootstrapPeers = func() []peer.AddrInfo {
return pis
}
return cfg
Expand All @@ -73,7 +72,7 @@ func BootstrapConfigWithPeers(pis []peerstore.PeerInfo) BootstrapConfig {
// check the number of open connections and -- if there are too few -- initiate
// connections to well-known bootstrap peers. It also kicks off subsystem
// bootstrapping (i.e. routing).
func Bootstrap(id peer.ID, host host.Host, rt routing.IpfsRouting, cfg BootstrapConfig) (io.Closer, error) {
func Bootstrap(id peer.ID, host host.Host, rt routing.Routing, cfg BootstrapConfig) (io.Closer, error) {

// make a signal to wait for one bootstrap round to complete.
doneWithRound := make(chan struct{})
Expand Down Expand Up @@ -135,9 +134,9 @@ func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) er
numToDial := cfg.MinPeerThreshold - len(connected)

// filter out bootstrap nodes we are already connected to
var notConnected []peerstore.PeerInfo
var notConnected []peer.AddrInfo
for _, p := range peers {
if host.Network().Connectedness(p.ID) != net.Connected {
if host.Network().Connectedness(p.ID) != network.Connected {
notConnected = append(notConnected, p)
}
}
Expand All @@ -156,7 +155,7 @@ func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) er
return bootstrapConnect(ctx, host, randSubset)
}

func bootstrapConnect(ctx context.Context, ph host.Host, peers []peerstore.PeerInfo) error {
func bootstrapConnect(ctx context.Context, ph host.Host, peers []peer.AddrInfo) error {
if len(peers) < 1 {
return ErrNotEnoughBootstrapPeers
}
Expand All @@ -171,7 +170,7 @@ func bootstrapConnect(ctx context.Context, ph host.Host, peers []peerstore.PeerI
// Also, performed asynchronously for dial speed.

wg.Add(1)
go func(p peerstore.PeerInfo) {
go func(p peer.AddrInfo) {
defer wg.Done()
defer log.EventBegin(ctx, "bootstrapDial", ph.ID(), p.ID).Done()
log.Debugf("%s bootstrapping to %s", ph.ID(), p.ID)
Expand Down Expand Up @@ -205,37 +204,14 @@ func bootstrapConnect(ctx context.Context, ph host.Host, peers []peerstore.PeerI
return nil
}

func randomSubsetOfPeers(in []peerstore.PeerInfo, max int) []peerstore.PeerInfo {
func randomSubsetOfPeers(in []peer.AddrInfo, max int) []peer.AddrInfo {
if max > len(in) {
max = len(in)
}

out := make([]peerstore.PeerInfo, max)
out := make([]peer.AddrInfo, max)
for i, val := range rand.Perm(len(in))[:max] {
out[i] = in[val]
}
return out
}

type Peers []config.BootstrapPeer

func (bpeers Peers) ToPeerInfos() []peerstore.PeerInfo {
pinfos := make(map[peer.ID]*peerstore.PeerInfo)
for _, bootstrap := range bpeers {
pinfo, ok := pinfos[bootstrap.ID()]
if !ok {
pinfo = new(peerstore.PeerInfo)
pinfos[bootstrap.ID()] = pinfo
pinfo.ID = bootstrap.ID()
}

pinfo.Addrs = append(pinfo.Addrs, bootstrap.Transport())
}

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

return peers
}
41 changes: 5 additions & 36 deletions core/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,25 @@
package bootstrap

import (
"fmt"
"testing"

config "github.com/ipfs/go-ipfs-config"
pstore "github.com/libp2p/go-libp2p-peerstore"
testutil "github.com/libp2p/go-testutil"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/test"
)

func TestSubsetWhenMaxIsGreaterThanLengthOfSlice(t *testing.T) {
var ps []pstore.PeerInfo
var ps []peer.AddrInfo
sizeofSlice := 100
for i := 0; i < sizeofSlice; i++ {
pid, err := testutil.RandPeerID()
pid, err := test.RandPeerID()
if err != nil {
t.Fatal(err)
}

ps = append(ps, pstore.PeerInfo{ID: pid})
ps = append(ps, peer.AddrInfo{ID: pid})
}
out := randomSubsetOfPeers(ps, 2*sizeofSlice)
if len(out) != len(ps) {
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 := Peers.ToPeerInfos(bsps)
if len(pinfos) != len(bsps)/2 {
t.Fatal("expected fewer peers")
}
}
2 changes: 1 addition & 1 deletion core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
decision "github.com/ipfs/go-bitswap/decision"
cidutil "github.com/ipfs/go-cidutil"
cmds "github.com/ipfs/go-ipfs-cmds"
peer "github.com/libp2p/go-libp2p-peer"
peer "github.com/libp2p/go-libp2p-core/peer"
)

var BitswapCmd = &cmds.Command{
Expand Down

0 comments on commit 1a32379

Please sign in to comment.