Skip to content

Commit

Permalink
split peerstore from peer package
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed May 31, 2016
1 parent df1c773 commit 916bc55
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 70 deletions.
5 changes: 3 additions & 2 deletions p2p/discovery/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/cryptix/mdns"
"github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
"github.com/ipfs/go-libp2p/p2p/host"
logging "github.com/ipfs/go-log"
ma "github.com/jbenet/go-multiaddr"
Expand All @@ -28,7 +29,7 @@ type Service interface {
}

type Notifee interface {
HandlePeerFound(peer.PeerInfo)
HandlePeerFound(pstore.PeerInfo)
}

type mdnsService struct {
Expand Down Expand Up @@ -154,7 +155,7 @@ func (m *mdnsService) handleEntry(e *mdns.ServiceEntry) {
return
}

pi := peer.PeerInfo{
pi := pstore.PeerInfo{
ID: mpeer,
Addrs: []ma.Multiaddr{maddr},
}
Expand Down
7 changes: 4 additions & 3 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"

peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
mstream "github.com/ipfs/go-libp2p/p2p/metrics/stream"
inet "github.com/ipfs/go-libp2p/p2p/net"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (h *BasicHost) ID() peer.ID {
}

// Peerstore returns the Host's repository of Peer Addresses and Keys.
func (h *BasicHost) Peerstore() peer.Peerstore {
func (h *BasicHost) Peerstore() pstore.Peerstore {
return h.Network().Peerstore()
}

Expand Down Expand Up @@ -181,10 +182,10 @@ func (h *BasicHost) NewStream(ctx context.Context, pid protocol.ID, p peer.ID) (
// peerstore. If there is not an active connection, Connect will issue a
// h.Network.Dial, and block until a connection is open, or an error is
// returned. // TODO: Relay + NAT.
func (h *BasicHost) Connect(ctx context.Context, pi peer.PeerInfo) error {
func (h *BasicHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {

// absorb addresses into peerstore
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peer.TempAddrTTL)
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)

cs := h.Network().ConnsToPeer(pi.ID)
if len(cs) > 0 {
Expand Down
5 changes: 3 additions & 2 deletions p2p/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package host

import (
peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
inet "github.com/ipfs/go-libp2p/p2p/net"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
Expand All @@ -23,7 +24,7 @@ type Host interface {
ID() peer.ID

// Peerstore returns the Host's repository of Peer Addresses and Keys.
Peerstore() peer.Peerstore
Peerstore() pstore.Peerstore

// Returns the listen addresses of the Host
Addrs() []ma.Multiaddr
Expand All @@ -39,7 +40,7 @@ type Host interface {
// peerstore. If there is not an active connection, Connect will issue a
// h.Network.Dial, and block until a connection is open, or an error is
// returned. // TODO: Relay + NAT.
Connect(ctx context.Context, pi peer.PeerInfo) error
Connect(ctx context.Context, pi pstore.PeerInfo) error

// SetStreamHandler sets the protocol handler on the Host's Mux.
// This is equivalent to:
Expand Down
9 changes: 5 additions & 4 deletions p2p/host/routed/routed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

lgbl "github.com/ipfs/go-libp2p-loggables"
peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
host "github.com/ipfs/go-libp2p/p2p/host"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
inet "github.com/ipfs/go-libp2p/p2p/net"
Expand All @@ -32,7 +33,7 @@ type RoutedHost struct {
}

type Routing interface {
FindPeer(context.Context, peer.ID) (peer.PeerInfo, error)
FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error)
}

func Wrap(h host.Host, r Routing) *RoutedHost {
Expand All @@ -44,15 +45,15 @@ func Wrap(h host.Host, r Routing) *RoutedHost {
//
// RoutedHost's Connect differs in that if the host has no addresses for a
// given peer, it will use its routing system to try to find some.
func (rh *RoutedHost) Connect(ctx context.Context, pi peer.PeerInfo) error {
func (rh *RoutedHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {
// first, check if we're already connected.
if len(rh.Network().ConnsToPeer(pi.ID)) > 0 {
return nil
}

// if we were given some addresses, keep + use them.
if len(pi.Addrs) > 0 {
rh.Peerstore().AddAddrs(pi.ID, pi.Addrs, peer.TempAddrTTL)
rh.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)
}

// Check if we have some addresses in our recent memory.
Expand Down Expand Up @@ -89,7 +90,7 @@ func (rh *RoutedHost) ID() peer.ID {
return rh.host.ID()
}

func (rh *RoutedHost) Peerstore() peer.Peerstore {
func (rh *RoutedHost) Peerstore() pstore.Peerstore {
return rh.host.Peerstore()
}

Expand Down
3 changes: 2 additions & 1 deletion p2p/net/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"

peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
conn "github.com/ipfs/go-libp2p/p2p/net/conn"
ma "github.com/jbenet/go-multiaddr"
"github.com/jbenet/goprocess"
Expand Down Expand Up @@ -91,7 +92,7 @@ type Dialer interface {
// Peerstore returns the internal peerstore
// This is useful to tell the dialer about a new address for a peer.
// Or use one of the public keys found out over the network.
Peerstore() peer.Peerstore
Peerstore() pstore.Peerstore

// LocalPeer returns the local peer associated with this network
LocalPeer() peer.ID
Expand Down
12 changes: 7 additions & 5 deletions p2p/net/mock/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package mocknet

import (
ic "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
host "github.com/ipfs/go-libp2p/p2p/host"
inet "github.com/ipfs/go-libp2p/p2p/net"
"io"
"time"

host "github.com/ipfs/go-libp2p/p2p/host"
inet "github.com/ipfs/go-libp2p/p2p/net"

ic "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
ma "github.com/jbenet/go-multiaddr"
)

Expand All @@ -25,7 +27,7 @@ type Mocknet interface {
// AddPeer adds an existing peer. we need both a privkey and addr.
// ID is derived from PrivKey
AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error)
AddPeerWithPeerstore(peer.ID, peer.Peerstore) (host.Host, error)
AddPeerWithPeerstore(peer.ID, pstore.Peerstore) (host.Host, error)

// retrieve things (with randomized iteration order)
Peers() []peer.ID
Expand Down
11 changes: 6 additions & 5 deletions p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"sort"
"sync"

ic "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
host "github.com/ipfs/go-libp2p/p2p/host"
bhost "github.com/ipfs/go-libp2p/p2p/host/basic"
inet "github.com/ipfs/go-libp2p/p2p/net"
p2putil "github.com/ipfs/go-libp2p/p2p/test/util"
testutil "github.com/ipfs/go-libp2p/testutil"

ic "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
ma "github.com/jbenet/go-multiaddr"
"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
Expand Down Expand Up @@ -69,15 +70,15 @@ func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
return nil, err
}

ps := peer.NewPeerstore()
ps.AddAddr(p, a, peer.PermanentAddrTTL)
ps := pstore.NewPeerstore()
ps.AddAddr(p, a, pstore.PermanentAddrTTL)
ps.AddPrivKey(p, k)
ps.AddPubKey(p, k.GetPublic())

return mn.AddPeerWithPeerstore(p, ps)
}

func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps peer.Peerstore) (host.Host, error) {
func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Host, error) {
n, err := newPeernet(mn.ctx, mn, p, ps)
if err != nil {
return nil, err
Expand Down
12 changes: 7 additions & 5 deletions p2p/net/mock/mock_peernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"math/rand"
"sync"

peer "github.com/ipfs/go-libp2p-peer"
inet "github.com/ipfs/go-libp2p/p2p/net"

peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
ma "github.com/jbenet/go-multiaddr"
"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
Expand All @@ -18,7 +20,7 @@ type peernet struct {
mocknet *mocknet // parent

peer peer.ID
ps peer.Peerstore
ps pstore.Peerstore

// conns are actual live connections between peers.
// many conns could run over each link.
Expand All @@ -38,7 +40,7 @@ type peernet struct {
}

// newPeernet constructs a new peernet
func newPeernet(ctx context.Context, m *mocknet, p peer.ID, ps peer.Peerstore) (*peernet, error) {
func newPeernet(ctx context.Context, m *mocknet, p peer.ID, ps pstore.Peerstore) (*peernet, error) {

n := &peernet{
mocknet: m,
Expand Down Expand Up @@ -82,7 +84,7 @@ func (pn *peernet) Close() error {
return pn.proc.Close()
}

func (pn *peernet) Peerstore() peer.Peerstore {
func (pn *peernet) Peerstore() pstore.Peerstore {
return pn.ps
}

Expand Down Expand Up @@ -293,7 +295,7 @@ func (pn *peernet) BandwidthTotals() (in uint64, out uint64) {

// Listen tells the network to start listening on given multiaddrs.
func (pn *peernet) Listen(addrs ...ma.Multiaddr) error {
pn.Peerstore().AddAddrs(pn.LocalPeer(), addrs, peer.PermanentAddrTTL)
pn.Peerstore().AddAddrs(pn.LocalPeer(), addrs, pstore.PermanentAddrTTL)
return nil
}

Expand Down
19 changes: 10 additions & 9 deletions p2p/net/swarm/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"testing"
"time"

peer "github.com/ipfs/go-libp2p-peer"
addrutil "github.com/ipfs/go-libp2p/p2p/net/swarm/addr"
testutil "github.com/ipfs/go-libp2p/testutil"
ci "github.com/ipfs/go-libp2p/testutil/ci"

peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
ma "github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-multiaddr-net"
context "golang.org/x/net/context"
Expand All @@ -32,7 +33,7 @@ func TestBasicDial(t *testing.T) {
s1 := swarms[0]
s2 := swarms[1]

s1.peers.AddAddrs(s2.local, s2.ListenAddresses(), peer.PermanentAddrTTL)
s1.peers.AddAddrs(s2.local, s2.ListenAddresses(), pstore.PermanentAddrTTL)

c, err := s1.Dial(ctx, s2.local)
if err != nil {
Expand All @@ -57,7 +58,7 @@ func TestDialWithNoListeners(t *testing.T) {
defer closeSwarms(swarms)
s2 := swarms[0]

s1.peers.AddAddrs(s2.local, s2.ListenAddresses(), peer.PermanentAddrTTL)
s1.peers.AddAddrs(s2.local, s2.ListenAddresses(), pstore.PermanentAddrTTL)

c, err := s1.Dial(ctx, s2.local)
if err != nil {
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestSimultDials(t *testing.T) {
connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
// copy for other peer
log.Debugf("TestSimultOpen: connecting: %s --> %s (%s)", s.local, dst, addr)
s.peers.AddAddr(dst, addr, peer.TempAddrTTL)
s.peers.AddAddr(dst, addr, pstore.TempAddrTTL)
if _, err := s.Dial(ctx, dst); err != nil {
t.Fatal("error swarm dialing to peer", err)
}
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestDialWait(t *testing.T) {
s2p, s2addr, s2l := newSilentPeer(t)
go acceptAndHang(s2l)
defer s2l.Close()
s1.peers.AddAddr(s2p, s2addr, peer.PermanentAddrTTL)
s1.peers.AddAddr(s2p, s2addr, pstore.PermanentAddrTTL)

before := time.Now()
if c, err := s1.Dial(ctx, s2p); err == nil {
Expand Down Expand Up @@ -224,13 +225,13 @@ func TestDialBackoff(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s1.peers.AddAddrs(s2.local, s2addrs, peer.PermanentAddrTTL)
s1.peers.AddAddrs(s2.local, s2addrs, pstore.PermanentAddrTTL)

// dial to a non-existent peer.
s3p, s3addr, s3l := newSilentPeer(t)
go acceptAndHang(s3l)
defer s3l.Close()
s1.peers.AddAddr(s3p, s3addr, peer.PermanentAddrTTL)
s1.peers.AddAddr(s3p, s3addr, pstore.PermanentAddrTTL)

// in this test we will:
// 1) dial 10x to each node.
Expand Down Expand Up @@ -442,7 +443,7 @@ func TestDialBackoffClears(t *testing.T) {
defer s2l.Close()

// phase 1 -- dial to non-operational addresses
s1.peers.AddAddr(s2.local, s2bad, peer.PermanentAddrTTL)
s1.peers.AddAddr(s2.local, s2bad, pstore.PermanentAddrTTL)

before := time.Now()
if c, err := s1.Dial(ctx, s2.local); err == nil {
Expand Down Expand Up @@ -472,7 +473,7 @@ func TestDialBackoffClears(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s1.peers.AddAddrs(s2.local, ifaceAddrs1, peer.PermanentAddrTTL)
s1.peers.AddAddrs(s2.local, ifaceAddrs1, pstore.PermanentAddrTTL)

if _, err := s1.Dial(ctx, s2.local); err == nil {
t.Fatal("should have failed to dial backed off peer")
Expand Down
3 changes: 2 additions & 1 deletion p2p/net/swarm/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
ma "github.com/jbenet/go-multiaddr"
context "golang.org/x/net/context"
)
Expand All @@ -17,7 +18,7 @@ func TestPeers(t *testing.T) {

connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
// TODO: make a DialAddr func.
s.peers.AddAddr(dst, addr, peer.PermanentAddrTTL)
s.peers.AddAddr(dst, addr, pstore.PermanentAddrTTL)
// t.Logf("connections from %s", s.LocalPeer())
// for _, c := range s.ConnectionsToPeer(dst) {
// t.Logf("connection from %s to %s: %v", s.LocalPeer(), dst, c)
Expand Down
5 changes: 3 additions & 2 deletions p2p/net/swarm/simul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"testing"
"time"

peer "github.com/ipfs/go-libp2p-peer"
ci "github.com/ipfs/go-libp2p/testutil/ci"

peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
ma "github.com/jbenet/go-multiaddr"
context "golang.org/x/net/context"
)
Expand All @@ -26,7 +27,7 @@ func TestSimultOpen(t *testing.T) {
connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
// copy for other peer
log.Debugf("TestSimultOpen: connecting: %s --> %s (%s)", s.local, dst, addr)
s.peers.AddAddr(dst, addr, peer.PermanentAddrTTL)
s.peers.AddAddr(dst, addr, pstore.PermanentAddrTTL)
if _, err := s.Dial(ctx, dst); err != nil {
t.Fatal("error swarm dialing to peer", err)
}
Expand Down
Loading

0 comments on commit 916bc55

Please sign in to comment.