peer+server: wait until peer is active to begin channel opening process

Without waiting, we would proceed to retrieve the remote peer's
supported features, which may have not been set due to not yet receiving
their Init message.
This commit is contained in:
Wilmer Paulino 2019-10-01 17:35:23 -04:00
parent 43b02d3035
commit cf5dc90d04
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
2 changed files with 21 additions and 0 deletions

View File

@ -121,6 +121,10 @@ type peer struct {
addr *lnwire.NetAddress addr *lnwire.NetAddress
pubKeyBytes [33]byte pubKeyBytes [33]byte
// activeSignal when closed signals that the peer is now active and
// ready to process messages.
activeSignal chan struct{}
// startTime is the time this peer connection was successfully // startTime is the time this peer connection was successfully
// established. It will be zero for peers that did not successfully // established. It will be zero for peers that did not successfully
// Start(). // Start().
@ -241,6 +245,8 @@ func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server,
conn: conn, conn: conn,
addr: addr, addr: addr,
activeSignal: make(chan struct{}),
inbound: inbound, inbound: inbound,
connReq: connReq, connReq: connReq,
@ -369,6 +375,9 @@ func (p *peer) Start() error {
go p.channelManager() go p.channelManager()
go p.pingHandler() go p.pingHandler()
// Signal to any external processes that the peer is now active.
close(p.activeSignal)
// Now that the peer has started up, we send any channel sync messages // Now that the peer has started up, we send any channel sync messages
// that must be resent for borked channels. // that must be resent for borked channels.
if len(msgs) > 0 { if len(msgs) > 0 {

View File

@ -3207,6 +3207,18 @@ func (s *server) OpenChannel(
} }
s.mu.RUnlock() s.mu.RUnlock()
// We'll wait until the peer is active before beginning the channel
// opening process.
select {
case <-peer.activeSignal:
case <-peer.quit:
req.err <- fmt.Errorf("peer %x disconnected", pubKeyBytes)
return req.updates, req.err
case <-s.quit:
req.err <- ErrServerShuttingDown
return req.updates, req.err
}
// If the fee rate wasn't specified, then we'll use a default // If the fee rate wasn't specified, then we'll use a default
// confirmation target. // confirmation target.
if req.fundingFeePerKw == 0 { if req.fundingFeePerKw == 0 {