Skip to content

Commit

Permalink
refactor(p2p): consolidate p2p struct around config
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Sep 15, 2018
1 parent 0e4915e commit e26be1b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var log = golog.Logger("lib")

// VersionNumber is the current version qri
const VersionNumber = "0.5.2"
const VersionNumber = "0.5.3-dev"

// Requests defines a set of library methods
type Requests interface {
Expand Down
3 changes: 1 addition & 2 deletions p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package p2p

import (
"context"
"fmt"
"math/rand"

ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
Expand Down Expand Up @@ -43,7 +42,7 @@ func (n *QriNode) Bootstrap(boostrapAddrs []string, boostrapPeers chan pstore.Pe
func (n *QriNode) BootstrapIPFS() {
if node, err := n.IPFSNode(); err == nil {
if err := node.Bootstrap(ipfscore.DefaultBootstrapConfig); err != nil {
fmt.Errorf("IPFS bootsrap error: %s", err.Error())
log.Errorf("IPFS bootsrap error: %s", err.Error())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error {
// Check our existing peerstore for any potential friends
go n.DiscoverPeerstoreQriPeers(n.Host.Peerstore())
// Boostrap off of default addresses
go n.Bootstrap(n.BootstrapAddrs, bootstrapPeers)
go n.Bootstrap(n.cfg.BootstrapAddrs, bootstrapPeers)
// Bootstrap to IPFS network if this node is using an IPFS fs
go n.BootstrapIPFS()

Expand Down
37 changes: 15 additions & 22 deletions p2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type QriNode struct {
// private key for encrypted communication & verifying identity
privateKey crypto.PrivKey

conf *config.P2P
cfg *config.P2P

// base context for this node
ctx context.Context

// Online indicates weather this is node is connected to the p2p network
Online bool
Expand All @@ -38,19 +41,13 @@ type QriNode struct {
// Discovery service, can be provided by an ipfs node
Discovery discovery.Service

// base context for this node
ctx context.Context

// Repo is a repository of this node's qri data
// note that repo's are built upon a cafs.Filestore, which
// may contain a reference to a functioning IPFS node. In that case
// QriNode should piggyback non-qri-specific p2p functionality on the
// ipfs node provided by repo
Repo repo.Repo

// BootstrapAddrs is a list of multiaddresses to bootrap *qri* from (not IPFS)
BootstrapAddrs []string

// handlers maps this nodes registered handlers. This works in a way similary to a router
// in traditional client/server models, but messages are flying around all over the place
// instead of a request/response pattern
Expand All @@ -62,8 +59,6 @@ type QriNode struct {
msgChan chan Message
// receivers is a list of anyone who wants to be notifed on new message arrival
receivers []chan Message
// profileReplication sets what to do when this node sees it's own profile
profileReplication string
}

// Assert that conversions needed by the tests are valid.
Expand All @@ -87,14 +82,12 @@ func NewQriNode(r repo.Repo, p2pconf *config.P2P) (node *QriNode, err error) {
}

node = &QriNode{
ID: pid,
conf: p2pconf,
Repo: r,
ctx: context.Background(),
BootstrapAddrs: p2pconf.QriBootstrapAddrs,
msgState: &sync.Map{},
msgChan: make(chan Message),
profileReplication: p2pconf.ProfileReplication,
ID: pid,
cfg: p2pconf,
Repo: r,
ctx: context.Background(),
msgState: &sync.Map{},
msgChan: make(chan Message),
}
node.handlers = MakeHandlers(node)

Expand All @@ -104,7 +97,7 @@ func NewQriNode(r repo.Repo, p2pconf *config.P2P) (node *QriNode, err error) {
// Connect allocates all networking structs to enable QriNode to communicate
// over
func (n *QriNode) Connect() (err error) {
if !n.conf.Enabled {
if !n.cfg.Enabled {
return fmt.Errorf("p2p connection is disabled")
}

Expand All @@ -131,7 +124,7 @@ func (n *QriNode) Connect() (err error) {
}
} else if n.Host == nil {
ps := pstore.NewPeerstore()
n.Host, err = makeBasicHost(n.ctx, ps, n.conf)
n.Host, err = makeBasicHost(n.ctx, ps, n.cfg)
if err != nil {
return fmt.Errorf("error creating host: %s", err.Error())
}
Expand Down Expand Up @@ -172,7 +165,7 @@ func (n *QriNode) StartOnlineServices(bootstrapped func(string)) error {
return nil
}

bsPeers := make(chan pstore.PeerInfo, len(n.BootstrapAddrs))
bsPeers := make(chan pstore.PeerInfo, len(n.cfg.BootstrapAddrs))
// need a call here to ensure boostrapped is called at least once
// TODO - this is an "original node" problem probably solved by being able
// to start a node with *no* qri peers specified.
Expand Down Expand Up @@ -362,8 +355,8 @@ func (n *QriNode) Addrs() pstore.AddrBook {
// SimplePeerInfo returns a PeerInfo with just the ID and Addresses.
func (n *QriNode) SimplePeerInfo() pstore.PeerInfo {
return pstore.PeerInfo{
n.Host.ID(),
n.Host.Addrs(),
ID: n.Host.ID(),
Addrs: n.Host.Addrs(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
// QriProtocolID is the top level Protocol Identifier
QriProtocolID = protocol.ID("/qri")
// QriServiceTag tags the type & version of the qri service
QriServiceTag = "qri/0.5.2"
QriServiceTag = "qri/0.5.3-dev"
// tag qri service uses in host connection Manager
qriConnManagerTag = "qri"
// default value to give qri peer connections in connmanager
Expand Down

0 comments on commit e26be1b

Please sign in to comment.