Commit 53f0b117 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

update net with peerstore

parent 0817ffa3
......@@ -106,7 +106,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
return nil, err
}
net, err = inet.NewIpfsNetwork(context.TODO(), local, &mux.ProtocolMap{
net, err = inet.NewIpfsNetwork(context.TODO(), local, peerstore, &mux.ProtocolMap{
mux.ProtocolID_Routing: dhtService,
mux.ProtocolID_Exchange: exchangeService,
})
......
......@@ -48,17 +48,6 @@ func NewConn(peer *peer.Peer, addr *ma.Multiaddr, nconn net.Conn) (*Conn, error)
return conn, nil
}
// NewNetConn constructs a new connection with given net.Conn
func NewNetConn(nconn net.Conn) (*Conn, error) {
addr, err := ma.FromNetAddr(nconn.RemoteAddr())
if err != nil {
return nil, err
}
return NewConn(new(peer.Peer), addr, nconn)
}
// Dial connects to a particular peer, over a given network
// Example: Dial("udp", peer)
func Dial(network string, peer *peer.Peer) (*Conn, error) {
......@@ -112,3 +101,9 @@ func (c *Conn) Close() error {
c.Closed <- true
return err
}
// NetConnMultiaddr returns the net.Conn's address, recast as a multiaddr.
// (consider moving this directly into the multiaddr package)
func NetConnMultiaddr(nconn net.Conn) (*ma.Multiaddr, error) {
return ma.FromNetAddr(nconn.RemoteAddr())
}
......@@ -30,7 +30,7 @@ type IpfsNetwork struct {
// NewIpfsNetwork is the structure that implements the network interface
func NewIpfsNetwork(ctx context.Context, local *peer.Peer,
pmap *mux.ProtocolMap) (*IpfsNetwork, error) {
peers peer.Peerstore, pmap *mux.ProtocolMap) (*IpfsNetwork, error) {
ctx, cancel := context.WithCancel(ctx)
......@@ -47,7 +47,7 @@ func NewIpfsNetwork(ctx context.Context, local *peer.Peer,
return nil, err
}
in.swarm, err = swarm.NewSwarm(ctx, local)
in.swarm, err = swarm.NewSwarm(ctx, local, peers)
if err != nil {
cancel()
return nil, err
......
......@@ -76,15 +76,19 @@ func (s *Swarm) connListen(maddr *ma.Multiaddr) error {
// Handle getting ID from this peer, handshake, and adding it into the map
func (s *Swarm) handleIncomingConn(nconn net.Conn) {
c, err := conn.NewNetConn(nconn)
addr, err := conn.NetConnMultiaddr(nconn)
if err != nil {
s.errChan <- err
return
}
//TODO(jbenet) the peer might potentially already be in the global PeerBook.
// maybe use the handshake to populate peer.
c.Peer.AddAddress(c.Addr)
// Construct conn with nil peer for now, because we don't know its ID yet.
// connSetup will figure this out, and pull out / construct the peer.
c, err := conn.NewConn(nil, addr, nconn)
if err != nil {
s.errChan <- err
return
}
// Setup the new connection
err = s.connSetup(c)
......@@ -101,7 +105,11 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
return errors.New("Tried to start nil connection.")
}
u.DOut("Starting connection: %s\n", c.Peer.Key().Pretty())
if c.Peer != nil {
u.DOut("Starting connection: %s\n", c.Peer.Key().Pretty())
} else {
u.DOut("Starting connection: [unknown peer]\n")
}
if err := s.connSecure(c); err != nil {
return fmt.Errorf("Conn securing error: %v", err)
......@@ -109,6 +117,10 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
u.DOut("Secured connection: %s\n", c.Peer.Key().Pretty())
//TODO(jbenet) the peer might potentially already be in the global PeerBook.
// maybe use the handshake to populate peer.
c.Peer.AddAddress(c.Addr)
// add to conns
s.connsLock.Lock()
if _, ok := s.conns[c.Peer.Key()]; ok {
......@@ -126,7 +138,7 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
// connSecure setups a secure remote connection.
func (s *Swarm) connSecure(c *conn.Conn) error {
sp, err := spipe.NewSecurePipe(s.ctx, 10, s.local, c.Peer)
sp, err := spipe.NewSecurePipe(s.ctx, 10, s.local, s.peers)
if err != nil {
return err
}
......@@ -139,6 +151,13 @@ func (s *Swarm) connSecure(c *conn.Conn) error {
return err
}
if c.Peer == nil {
c.Peer = sp.RemotePeer()
} else if c.Peer != sp.RemotePeer() {
panic("peers not being constructed correctly.")
}
c.Secure = sp
return nil
}
......
......@@ -46,6 +46,9 @@ type Swarm struct {
// local is the peer this swarm represents
local *peer.Peer
// peers is a collection of peers for swarm to use
peers peer.Peerstore
// Swarm includes a Pipe object.
*msg.Pipe
......@@ -65,11 +68,12 @@ type Swarm struct {
}
// NewSwarm constructs a Swarm, with a Chan.
func NewSwarm(ctx context.Context, local *peer.Peer) (*Swarm, error) {
func NewSwarm(ctx context.Context, local *peer.Peer, ps peer.Peerstore) (*Swarm, error) {
s := &Swarm{
Pipe: msg.NewPipe(10),
conns: conn.Map{},
local: local,
peers: ps,
errChan: make(chan error, 100),
}
......@@ -112,9 +116,18 @@ func (s *Swarm) Dial(peer *peer.Peer) (*conn.Conn, error) {
// check if we already have an open connection first
c := s.GetConnection(peer.ID)
if c != nil {
return c, nil
}
// check if we don't have the peer in Peerstore
err := s.peers.Put(peer)
if err != nil {
return nil, err
}
// open connection to peer
c, err := conn.Dial("tcp", peer)
c, err = conn.Dial("tcp", peer)
if err != nil {
return nil, err
}
......
......@@ -31,7 +31,7 @@ func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
t.Fatal(err)
}
net, err := inet.NewIpfsNetwork(ctx, p, &mux.ProtocolMap{
net, err := inet.NewIpfsNetwork(ctx, p, peerstore, &mux.ProtocolMap{
mux.ProtocolID_Routing: dhts,
})
if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment