Skip to content

Commit

Permalink
fix(p2p.Bootstrap): fixes to bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Dec 11, 2017
1 parent 314f088 commit 351c8dd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
22 changes: 14 additions & 8 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -41,14 +41,20 @@ func cachePath() string {
}

func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
dir, err := homedir.Dir()
if err != nil {
panic(err)
}
return os.Getenv("HOME")
return dir

// if runtime.GOOS == "windows" {
// home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
// if home == "" {
// home = os.Getenv("USERPROFILE")
// }
// return home
// }
// return os.Getenv("HOME")
}

func loadFileIfPath(path string) (file *os.File, err error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func initConfig() (created bool) {

ipfsFsPath := os.Getenv("IPFS_PATH")
if ipfsFsPath == "" {
ipfsFsPath = "$HOME/.ipfs"
ipfsFsPath = filepath.Join(home, ".ipfs")
}
ipfsFsPath = strings.Replace(ipfsFsPath, "~", home, 1)
viper.SetDefault(IpfsFsPath, ipfsFsPath)
Expand Down
16 changes: 8 additions & 8 deletions p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// This boostrapping is specific to finding qri peers, which are IPFS peers that also
// support the qri protocol.
// (we also perform standard IPFS boostrapping when IPFS networking is enabled, and it's almost always enabled).
// These are addresses to public, qri nodes hosted by qri.
// These are addresses to public qri nodes hosted by qri, inc.
// One day it would be super nice to bootstrap from a stored history & only
// use these for first-round bootstrapping.
var DefaultBootstrapAddresses = []string{
Expand All @@ -33,18 +33,18 @@ func (n *QriNode) Bootstrap(boostrapAddrs []string) {

pinfos := toPeerInfos(peers)

for _, pi := range randomSubsetOfPeers(pinfos, 4) {
go func() {
if err := n.Host.Connect(context.Background(), pi); err == nil {
n.log.Infof("boostrapping to: %s", pi.ID.Pretty())
if err = n.AddQriPeer(pi); err != nil {
for _, p := range randomSubsetOfPeers(pinfos, 4) {
go func(p pstore.PeerInfo) {
n.Host.Peerstore().AddAddrs(p.ID, p.Addrs, pstore.RecentlyConnectedAddrTTL)
if err := n.Host.Connect(context.Background(), p); err == nil {
n.log.Infof("boostrapping to: %s", p.ID.Pretty())
if err = n.AddQriPeer(p); err != nil {
n.log.Infof("error adding peer: %s", err.Error())
}
n.RequestPeersList(pi.ID)
} else {
n.log.Infof("error connecting to host: %s", err.Error())
}
}()
}(p)
}
}

Expand Down
12 changes: 6 additions & 6 deletions p2p/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package p2p
import (
"context"
"fmt"
"time"
// "time"

pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
Expand All @@ -13,18 +13,18 @@ func (n *QriNode) AddQriPeer(pinfo pstore.PeerInfo) error {
// add this peer to our store
n.QriPeers.AddAddrs(pinfo.ID, pinfo.Addrs, pstore.TempAddrTTL)

if profile, _ := n.Repo.Peers().GetPeer(pinfo.ID); profile != nil {
// we've already seen this peer
return nil
}
// if profile, _ := n.Repo.Peers().GetPeer(pinfo.ID); profile != nil {
// // we've already seen this peer
// return nil
// }

if err := n.RequestProfileInfo(pinfo); err != nil {
return err
}

// some time later ask for a list of their peers, you know, "for a friend"
go func() {
time.Sleep(time.Second * 2)
// time.Sleep(time.Second * 2)
n.RequestPeersList(pinfo.ID)
}()

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
},
"gxDependencies": [
{
"hash": "QmdKL1GVaUaDVt3JUWiYQSLYRsJMym2KRWxsiXAeEU6pzX",
"hash": "QmViBzgruNUoLNBnXcx8YWbDNwV8MNGEGKkLo6JGetygdw",
"name": "go-ipfs",
"version": "0.4.12"
"version": "0.4.13"
},
{
"author": "whyrusleeping",
Expand Down

0 comments on commit 351c8dd

Please sign in to comment.