peer: fix panic during peer connection

This commit is contained in:
Andrey Samokhvalov 2017-02-22 12:10:07 +03:00 committed by Olaoluwa Osuntokun
parent b21bd351e8
commit 4c4ce93730
2 changed files with 15 additions and 15 deletions

@ -168,8 +168,8 @@ type peer struct {
// newPeer creates a new peer from an establish connection object, and a // newPeer creates a new peer from an establish connection object, and a
// pointer to the main server. // pointer to the main server.
func newPeer(conn net.Conn, server *server, addr *lnwire.NetAddress, func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server,
inbound bool) (*peer, error) { addr *lnwire.NetAddress, inbound bool) (*peer, error) {
nodePub := addr.IdentityKey nodePub := addr.IdentityKey
@ -180,6 +180,7 @@ func newPeer(conn net.Conn, server *server, addr *lnwire.NetAddress,
id: atomic.AddInt32(&numNodes, 1), id: atomic.AddInt32(&numNodes, 1),
inbound: inbound, inbound: inbound,
connReq: connReq,
server: server, server: server,
@ -209,7 +210,7 @@ func newPeer(conn net.Conn, server *server, addr *lnwire.NetAddress,
// Initiate the pending channel identifier properly depending on if this // Initiate the pending channel identifier properly depending on if this
// node is inbound or outbound. This value will be used in an increasing // node is inbound or outbound. This value will be used in an increasing
// manner to track pending channels. // manner to track pending channels.
if inbound { if p.inbound {
p.nextPendingChannelID = 1 << 63 p.nextPendingChannelID = 1 << 63
} else { } else {
p.nextPendingChannelID = 0 p.nextPendingChannelID = 0

@ -438,30 +438,29 @@ func (s *server) peerConnected(conn net.Conn, connReq *connmgr.ConnReq, inbound
// Now that we've established a connection, create a peer, and // Now that we've established a connection, create a peer, and
// it to the set of currently active peers. // it to the set of currently active peers.
peer, err := newPeer(conn, s, peerAddr, false) p, err := newPeer(conn, connReq, s, peerAddr, inbound)
if err != nil { if err != nil {
srvrLog.Errorf("unable to create peer %v", err) srvrLog.Errorf("unable to create peer %v", err)
s.connMgr.Remove(peer.connReq.ID()) if p.connReq != nil {
peer.Disconnect() s.connMgr.Remove(p.connReq.ID())
conn.Close() }
p.Disconnect()
return return
} }
if connReq != nil {
peer.connReq = connReq
}
// TODO(roasbeef): update IP address for link-node // TODO(roasbeef): update IP address for link-node
// * also mark last-seen, do it one single transaction? // * also mark last-seen, do it one single transaction?
if err := peer.Start(); err != nil { if err := p.Start(); err != nil {
srvrLog.Errorf("unable to start peer: %v", err) srvrLog.Errorf("unable to start peer: %v", err)
s.connMgr.Remove(peer.connReq.ID()) if p.connReq != nil {
peer.Disconnect() s.connMgr.Remove(p.connReq.ID())
conn.Close() }
p.Disconnect()
return return
} }
s.newPeers <- peer s.newPeers <- p
} }
// inboundPeerConnected initializes a new peer in response to a new inbound // inboundPeerConnected initializes a new peer in response to a new inbound