Skip to content

Commit

Permalink
fix(fsrepo.Peerstore): fix peerstore writing invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Apr 5, 2018
1 parent abcd937 commit b76c52b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 87 deletions.
7 changes: 0 additions & 7 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,6 @@ overwrite this info.`,

err = cfg.WriteToFile(configFilepath())
ExitIfErr(err)

// call SetProfile to give repo a chance to save updated profile data
pro, err := cfg.Profile.DecodeProfile()
ExitIfErr(err)
getRepo(false).SetProfile(pro)

ExitIfErr(err)
},
}

Expand Down
2 changes: 0 additions & 2 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const qriSupportKey = "qri-support"
// StartDiscovery initiates peer discovery, allocating a discovery
// services if one doesn't exist, then registering to be notified on peer discovery
func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error {
log.Info("starting peer discovery")

if n.Discovery == nil {
service, err := discovery.NewMdnsService(context.Background(), n.Host, time.Second*5, QriServiceTag)
if err != nil {
Expand Down
76 changes: 0 additions & 76 deletions p2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,56 +206,6 @@ func (n *QriNode) EncapsulatedAddresses() []ma.Multiaddr {
return res
}

// Peers returns a list of currently connected peer IDs
func (n *QriNode) Peers() []peer.ID {
if n.Host == nil {
return []peer.ID{}
}
conns := n.Host.Network().Conns()
seen := make(map[peer.ID]struct{})
peers := make([]peer.ID, 0, len(conns))

for _, c := range conns {
p := c.LocalPeer()
if _, found := seen[p]; found {
continue
}

seen[p] = struct{}{}
peers = append(peers, p)
}

return peers
}

// AddQriPeer negotiates a connection with a peer to get their profile details
// and peer list.
func (n *QriNode) AddQriPeer(pinfo pstore.PeerInfo) error {
// add this peer to our store
n.QriPeers.AddAddrs(pinfo.ID, pinfo.Addrs, pstore.TempAddrTTL)

if _, err := n.RequestProfile(pinfo.ID); err != nil {
log.Debug(err.Error())
return err
}

return nil
}

// ConnectedPeers lists all IPFS connected peers
func (n *QriNode) ConnectedPeers() []string {
if n.Host == nil {
return []string{}
}
conns := n.Host.Network().Conns()
peers := make([]string, len(conns))
for i, c := range conns {
peers[i] = c.RemotePeer().Pretty()
}

return peers
}

// ConnectedQriProfiles lists all connected peers that support the qri protocol
func (n *QriNode) ConnectedQriProfiles() map[profile.ID]*profile.Profile {
if n.Host == nil {
Expand All @@ -272,32 +222,6 @@ func (n *QriNode) ConnectedQriProfiles() map[profile.ID]*profile.Profile {
return peers
}

// ConnectToPeer takes a raw peer ID & tries to work out a route to that
// peer, explicitly connecting to them.
func (n *QriNode) ConnectToPeer(pid peer.ID) error {
// first check for local peer info
if pinfo := n.Host.Peerstore().PeerInfo(pid); pinfo.ID.String() != "" {
_, err := n.RequestProfile(pinfo.ID)
return err
}

// attempt to use ipfs routing table to discover peer
ipfsnode, err := n.IPFSNode()
if err != nil {
log.Debug(err.Error())
return err
}

pinfo, err := ipfsnode.Routing.FindPeer(context.Background(), pid)
if err != nil {
log.Debug(err.Error())
return err
}

_, err = n.RequestProfile(pinfo.ID)
return err
}

// Context returns this node's context
func (n *QriNode) Context() context.Context {
if n.ctx == nil {
Expand Down
79 changes: 79 additions & 0 deletions p2p/peers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package p2p

import (
"context"

"github.com/qri-io/qri/repo/profile"

pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
)

Expand Down Expand Up @@ -35,3 +38,79 @@ func (n *QriNode) ClosestConnectedPeers(id profile.ID, max int) (pid []peer.ID)

return
}

// AddQriPeer negotiates a connection with a peer to get their profile details
// and peer list.
func (n *QriNode) AddQriPeer(pinfo pstore.PeerInfo) error {
// add this peer to our store
n.QriPeers.AddAddrs(pinfo.ID, pinfo.Addrs, pstore.TempAddrTTL)

if _, err := n.RequestProfile(pinfo.ID); err != nil {
log.Debug(err.Error())
return err
}

return nil
}

// Peers returns a list of currently connected peer IDs
func (n *QriNode) Peers() []peer.ID {
if n.Host == nil {
return []peer.ID{}
}
conns := n.Host.Network().Conns()
seen := make(map[peer.ID]struct{})
peers := make([]peer.ID, 0, len(conns))

for _, c := range conns {
p := c.LocalPeer()
if _, found := seen[p]; found {
continue
}

seen[p] = struct{}{}
peers = append(peers, p)
}

return peers
}

// ConnectedPeers lists all IPFS connected peers
func (n *QriNode) ConnectedPeers() []string {
if n.Host == nil {
return []string{}
}
conns := n.Host.Network().Conns()
peers := make([]string, len(conns))
for i, c := range conns {
peers[i] = c.RemotePeer().Pretty()
}

return peers
}

// ConnectToPeer takes a raw peer ID & tries to work out a route to that
// peer, explicitly connecting to them.
func (n *QriNode) ConnectToPeer(pid peer.ID) error {
// first check for local peer info
if pinfo := n.Host.Peerstore().PeerInfo(pid); pinfo.ID.String() != "" {
_, err := n.RequestProfile(pinfo.ID)
return err
}

// attempt to use ipfs routing table to discover peer
ipfsnode, err := n.IPFSNode()
if err != nil {
log.Debug(err.Error())
return err
}

pinfo, err := ipfsnode.Routing.FindPeer(context.Background(), pid)
if err != nil {
log.Debug(err.Error())
return err
}

_, err = n.RequestProfile(pinfo.ID)
return err
}
20 changes: 19 additions & 1 deletion repo/fs/profile_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,22 @@ func (r ProfileStore) DeleteProfile(id profile.ID) error {
return r.saveFile(ps, FilePeers)
}

func (r ProfileStore) saveFile(ps map[profile.ID]*profile.Profile, f File) error {
pss := map[string]*profile.Profile{}
for _, p := range ps {
pss[p.ID.String()] = p
}

data, err := json.Marshal(pss)
if err != nil {
log.Debug(err.Error())
return err
}
return ioutil.WriteFile(r.filepath(f), data, os.ModePerm)
}

func (r *ProfileStore) profiles() (map[profile.ID]*profile.Profile, error) {
pss := map[string]*profile.Profile{}
ps := map[profile.ID]*profile.Profile{}
data, err := ioutil.ReadFile(r.filepath(FilePeers))
if err != nil {
Expand All @@ -132,9 +147,12 @@ func (r *ProfileStore) profiles() (map[profile.ID]*profile.Profile, error) {
return ps, fmt.Errorf("error loading peers: %s", err.Error())
}

if err := json.Unmarshal(data, &ps); err != nil {
if err := json.Unmarshal(data, &pss); err != nil {
log.Debug(err.Error())
return ps, fmt.Errorf("error unmarshaling peers: %s", err.Error())
}
for _, p := range pss {
ps[p.ID] = p
}
return ps, nil
}
2 changes: 1 addition & 1 deletion repo/profile/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// ID is a distinct thing form a peer.ID. They are *NOT* meant to be interchangable
// but the mechanics of peer.ID & profile.ID are exactly the same (for now).
// but the mechanics of peer.ID & profile.ID are exactly the same
type ID peer.ID

// String implements the stringer interface for ID
Expand Down

0 comments on commit b76c52b

Please sign in to comment.