Skip to content

Commit

Permalink
migrate to go-libp2p-core.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Raúl Kripalani <raul@protocol.ai>
  • Loading branch information
raulk authored and Stebalien committed May 31, 2019
1 parent 21b1547 commit 9be35fe
Show file tree
Hide file tree
Showing 79 changed files with 647 additions and 533 deletions.
8 changes: 4 additions & 4 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"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
secio "github.com/libp2p/go-libp2p-secio"
)
Expand Down Expand Up @@ -112,7 +112,7 @@ func main() {
}
}

func setupPeer(a args) (peer.ID, pstore.Peerstore, error) {
func setupPeer(a args) (peer.ID, peerstore.Peerstore, error) {
if a.keybits < 1024 {
return "", nil, errors.New("bitsize less than 1024 is considered unsafe")
}
Expand Down
39 changes: 20 additions & 19 deletions core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
"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 +52,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 +62,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 +74,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 +136,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 +157,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 +172,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,12 +206,12 @@ 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]
}
Expand All @@ -219,20 +220,20 @@ func randomSubsetOfPeers(in []peerstore.PeerInfo, max int) []peerstore.PeerInfo

type Peers []config.BootstrapPeer

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

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

var peers []peerstore.PeerInfo
var peers []peer.AddrInfo
for _, pinfo := range pinfos {
peers = append(peers, *pinfo)
}
Expand Down
12 changes: 6 additions & 6 deletions core/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"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) {
Expand All @@ -29,7 +29,7 @@ func TestSubsetWhenMaxIsGreaterThanLengthOfSlice(t *testing.T) {
func TestMultipleAddrsPerPeer(t *testing.T) {
var bsps []config.BootstrapPeer
for i := 0; i < 10; i++ {
pid, err := testutil.RandPeerID()
pid, err := test.RandPeerID()
if err != nil {
t.Fatal(err)
}
Expand Down
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 9be35fe

Please sign in to comment.