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
// pointer to the main server.
func newPeer(conn net.Conn, server *server, addr *lnwire.NetAddress,
inbound bool) (*peer, error) {
func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server,
addr *lnwire.NetAddress, inbound bool) (*peer, error) {
nodePub := addr.IdentityKey
@ -180,6 +180,7 @@ func newPeer(conn net.Conn, server *server, addr *lnwire.NetAddress,
id: atomic.AddInt32(&numNodes, 1),
inbound: inbound,
connReq: connReq,
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
// node is inbound or outbound. This value will be used in an increasing
// manner to track pending channels.
if inbound {
if p.inbound {
p.nextPendingChannelID = 1 << 63
} else {
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
// 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 {
srvrLog.Errorf("unable to create peer %v", err)
s.connMgr.Remove(peer.connReq.ID())
peer.Disconnect()
conn.Close()
if p.connReq != nil {
s.connMgr.Remove(p.connReq.ID())
}
p.Disconnect()
return
}
if connReq != nil {
peer.connReq = connReq
}
// TODO(roasbeef): update IP address for link-node
// * 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)
s.connMgr.Remove(peer.connReq.ID())
peer.Disconnect()
conn.Close()
if p.connReq != nil {
s.connMgr.Remove(p.connReq.ID())
}
p.Disconnect()
return
}
s.newPeers <- peer
s.newPeers <- p
}
// inboundPeerConnected initializes a new peer in response to a new inbound