Skip to content

Commit c8b081e

Browse files
authored
Merge pull request ipfs/kubo#6384 from ipfs/libp2p-core
migrate to go-libp2p-core. This commit was moved from ipfs/kubo@1a32379
2 parents ae7d192 + 6fb21aa commit c8b081e

File tree

2 files changed

+20
-75
lines changed

2 files changed

+20
-75
lines changed

core/bootstrap/bootstrap.go

+15-39
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ import (
99
"sync"
1010
"time"
1111

12-
config "github.com/ipfs/go-ipfs-config"
1312
logging "github.com/ipfs/go-log"
1413
"github.com/jbenet/goprocess"
1514
"github.com/jbenet/goprocess/context"
1615
"github.com/jbenet/goprocess/periodic"
17-
"github.com/libp2p/go-libp2p-host"
16+
"github.com/libp2p/go-libp2p-core/host"
17+
"github.com/libp2p/go-libp2p-core/network"
18+
"github.com/libp2p/go-libp2p-core/peer"
19+
"github.com/libp2p/go-libp2p-core/peerstore"
20+
"github.com/libp2p/go-libp2p-core/routing"
1821
"github.com/libp2p/go-libp2p-loggables"
19-
"github.com/libp2p/go-libp2p-net"
20-
"github.com/libp2p/go-libp2p-peer"
21-
"github.com/libp2p/go-libp2p-peerstore"
22-
"github.com/libp2p/go-libp2p-routing"
2322
)
2423

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

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

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

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

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

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

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

208-
func randomSubsetOfPeers(in []peerstore.PeerInfo, max int) []peerstore.PeerInfo {
207+
func randomSubsetOfPeers(in []peer.AddrInfo, max int) []peer.AddrInfo {
209208
if max > len(in) {
210209
max = len(in)
211210
}
212211

213-
out := make([]peerstore.PeerInfo, max)
212+
out := make([]peer.AddrInfo, max)
214213
for i, val := range rand.Perm(len(in))[:max] {
215214
out[i] = in[val]
216215
}
217216
return out
218217
}
219-
220-
type Peers []config.BootstrapPeer
221-
222-
func (bpeers Peers) ToPeerInfos() []peerstore.PeerInfo {
223-
pinfos := make(map[peer.ID]*peerstore.PeerInfo)
224-
for _, bootstrap := range bpeers {
225-
pinfo, ok := pinfos[bootstrap.ID()]
226-
if !ok {
227-
pinfo = new(peerstore.PeerInfo)
228-
pinfos[bootstrap.ID()] = pinfo
229-
pinfo.ID = bootstrap.ID()
230-
}
231-
232-
pinfo.Addrs = append(pinfo.Addrs, bootstrap.Transport())
233-
}
234-
235-
var peers []peerstore.PeerInfo
236-
for _, pinfo := range pinfos {
237-
peers = append(peers, *pinfo)
238-
}
239-
240-
return peers
241-
}

core/bootstrap/bootstrap_test.go

+5-36
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,25 @@
11
package bootstrap
22

33
import (
4-
"fmt"
54
"testing"
65

7-
config "github.com/ipfs/go-ipfs-config"
8-
pstore "github.com/libp2p/go-libp2p-peerstore"
9-
testutil "github.com/libp2p/go-testutil"
6+
"github.com/libp2p/go-libp2p-core/peer"
7+
"github.com/libp2p/go-libp2p-core/test"
108
)
119

1210
func TestSubsetWhenMaxIsGreaterThanLengthOfSlice(t *testing.T) {
13-
var ps []pstore.PeerInfo
11+
var ps []peer.AddrInfo
1412
sizeofSlice := 100
1513
for i := 0; i < sizeofSlice; i++ {
16-
pid, err := testutil.RandPeerID()
14+
pid, err := test.RandPeerID()
1715
if err != nil {
1816
t.Fatal(err)
1917
}
2018

21-
ps = append(ps, pstore.PeerInfo{ID: pid})
19+
ps = append(ps, peer.AddrInfo{ID: pid})
2220
}
2321
out := randomSubsetOfPeers(ps, 2*sizeofSlice)
2422
if len(out) != len(ps) {
2523
t.Fail()
2624
}
2725
}
28-
29-
func TestMultipleAddrsPerPeer(t *testing.T) {
30-
var bsps []config.BootstrapPeer
31-
for i := 0; i < 10; i++ {
32-
pid, err := testutil.RandPeerID()
33-
if err != nil {
34-
t.Fatal(err)
35-
}
36-
37-
addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/5001/ipfs/%s", pid.Pretty())
38-
bsp1, err := config.ParseBootstrapPeer(addr)
39-
if err != nil {
40-
t.Fatal(err)
41-
}
42-
43-
addr = fmt.Sprintf("/ip4/127.0.0.1/udp/5002/utp/ipfs/%s", pid.Pretty())
44-
bsp2, err := config.ParseBootstrapPeer(addr)
45-
if err != nil {
46-
t.Fatal(err)
47-
}
48-
49-
bsps = append(bsps, bsp1, bsp2)
50-
}
51-
52-
pinfos := Peers.ToPeerInfos(bsps)
53-
if len(pinfos) != len(bsps)/2 {
54-
t.Fatal("expected fewer peers")
55-
}
56-
}

0 commit comments

Comments
 (0)