Skip to content

Commit bd7e480

Browse files
committed
Read IPFS peers from existing config
1 parent dfca5c5 commit bd7e480

File tree

2 files changed

+26
-57
lines changed

2 files changed

+26
-57
lines changed

cmd/ipfs/daemon.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,15 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
291291
return fmt.Errorf("fs-repo requires migration")
292292
}
293293

294-
// TODO: Get optional peers to connect to.
295-
// - Read from existing config?
296-
// - Read from cli?
297-
// - Both?
298-
var peers []string
294+
// Read from existing config
295+
cfg, err := cctx.GetConfig()
296+
if err != nil {
297+
return fmt.Errorf("migrate: GetConfig() failed: %s", err)
298+
}
299299

300300
// Fetch migrations from current distribution, or location from environ
301301
fetchHttp := migrations.NewHttpFetcher(migrations.GetDistPathEnv(migrations.CurrentIpfsDist), "", "go-ipfs", 0)
302-
fetchIpfs := ipfsfetcher.NewIpfsFetcher(migrations.GetDistPathEnv(migrations.CurrentIpfsDist), 0, peers)
302+
fetchIpfs := ipfsfetcher.NewIpfsFetcher(migrations.GetDistPathEnv(migrations.CurrentIpfsDist), 0, cfg.Peering.Peers)
303303

304304
var (
305305
f1 migrations.Fetcher = fetchHttp

repo/fsrepo/migrations/ipfsfetcher/ipfsfetcher.go

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/ipfs/interface-go-ipfs-core/options"
2525
ipath "github.com/ipfs/interface-go-ipfs-core/path"
2626
peer "github.com/libp2p/go-libp2p-core/peer"
27-
ma "github.com/multiformats/go-multiaddr"
2827
)
2928

3029
const (
@@ -35,7 +34,7 @@ const (
3534
type IpfsFetcher struct {
3635
distPath string
3736
limit int64
38-
peers []string
37+
peers []peer.AddrInfo
3938

4039
openOnce sync.Once
4140
openErr error
@@ -51,7 +50,7 @@ type IpfsFetcher struct {
5150
//
5251
// Specifying "" for distPath sets the default IPNS path.
5352
// Specifying 0 for fetchLimit sets the default, -1 means no limit.
54-
func NewIpfsFetcher(distPath string, fetchLimit int64, peers []string) *IpfsFetcher {
53+
func NewIpfsFetcher(distPath string, fetchLimit int64, peers []peer.AddrInfo) *IpfsFetcher {
5554
f := &IpfsFetcher{
5655
limit: defaultFetchLimit,
5756
distPath: migrations.LatestIpfsDist,
@@ -167,7 +166,7 @@ func initTempNode(ctx context.Context) (string, error) {
167166
return dir, nil
168167
}
169168

170-
func startTempNode(repoDir string, peers []string) (iface.CoreAPI, func(), error) {
169+
func startTempNode(repoDir string, peers []peer.AddrInfo) (iface.CoreAPI, func(), error) {
171170
// Open the repo
172171
r, err := fsrepo.Open(repoDir)
173172
if err != nil {
@@ -202,13 +201,9 @@ func startTempNode(repoDir string, peers []string) (iface.CoreAPI, func(), error
202201
<-node.Context().Done()
203202
}
204203

204+
// Parse peer addresses and asynchronously connect to peers
205205
if len(peers) != 0 {
206-
// Asynchronously connect to any specified peers
207-
go func() {
208-
if err := connect(ctxIpfsLife, ifaceCore, peers); err != nil {
209-
fmt.Fprintf(os.Stderr, "failed to connect to peers: %s", err)
210-
}
211-
}()
206+
connectPeers(ctxIpfsLife, ifaceCore, peers)
212207
}
213208

214209
return ifaceCore, stopFunc, nil
@@ -269,46 +264,20 @@ func setupPlugins() error {
269264
return nil
270265
}
271266

272-
func connect(ctx context.Context, ipfs iface.CoreAPI, peers []string) error {
273-
pinfos := make(map[peer.ID]*peer.AddrInfo, len(peers))
274-
for _, addrStr := range peers {
275-
addr, err := ma.NewMultiaddr(addrStr)
276-
if err != nil {
277-
return err
278-
}
279-
pii, err := peer.AddrInfoFromP2pAddr(addr)
280-
if err != nil {
281-
return err
282-
}
283-
pi, ok := pinfos[pii.ID]
284-
if !ok {
285-
pi = &peer.AddrInfo{ID: pii.ID}
286-
pinfos[pi.ID] = pi
287-
}
288-
pi.Addrs = append(pi.Addrs, pii.Addrs...)
289-
}
290-
291-
connErrs := make(chan error)
292-
for _, pi := range pinfos {
293-
go func(pi *peer.AddrInfo) {
294-
if err := ipfs.Swarm().Connect(ctx, *pi); err != nil {
295-
connErrs <- fmt.Errorf("cound not connec to %q: %s", pi.ID, err)
296-
} else {
297-
connErrs <- nil
298-
}
299-
}(pi)
300-
}
301-
302-
var fails []string
303-
for i := 0; i < len(pinfos); i++ {
304-
err := <-connErrs
305-
if err != nil {
306-
fails = append(fails, err.Error())
267+
func connectPeers(ctx context.Context, ipfs iface.CoreAPI, peers []peer.AddrInfo) {
268+
// Asynchronously connect to each peer
269+
//
270+
// Do not return an error if there is a failure to connect to a peer, since
271+
// node may still be able to operate. Only write the errors to stderr.
272+
go func() {
273+
for i := range peers {
274+
go func(pi peer.AddrInfo) {
275+
if err := ipfs.Swarm().Connect(ctx, pi); err != nil {
276+
fmt.Fprintf(os.Stderr, "cound not connec to %q: %s\n", pi.ID, err)
277+
} else {
278+
fmt.Fprintf(os.Stderr, "conneced to peer %q\n", pi.ID)
279+
}
280+
}(peers[i])
307281
}
308-
}
309-
if len(fails) != 0 {
310-
return fmt.Errorf(strings.Join(fails, ", "))
311-
}
312-
313-
return nil
282+
}()
314283
}

0 commit comments

Comments
 (0)