From e1d8c37708a8e2723f9ea53a21018ed861691629 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 11 Jun 2018 23:52:38 -0700 Subject: [PATCH] peer: only add an active link to the channelManager if addPeer succeeds In this commit we fix an existing bug which could cause internal state inconsistency between then switch, funding manager, and the peer. Before this commit, we would _always_ add a new channel to the channelManager. However, due to recent logic, it may be the case that this isn't the channel that will ultimately reside in the link. As a result, we would be unable to process incoming FundingLocked messages properly, as we would mutate the incorrect channel in memory. We remedy this by moving the inserting of the new channel into the activeChannels map until the end of the loadActiveChannels method, where we know that this will be the link that persists. --- peer.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/peer.go b/peer.go index ba9642ea..b5996499 100644 --- a/peer.go +++ b/peer.go @@ -324,10 +324,6 @@ func (p *peer) loadActiveChannels(chans []*channeldb.OpenChannel) error { chanID := lnwire.NewChanIDFromOutPoint(chanPoint) - p.activeChanMtx.Lock() - p.activeChannels[chanID] = lnChan - p.activeChanMtx.Unlock() - peerLog.Infof("NodeKey(%x) loading ChannelPoint(%v)", p.PubKey(), chanPoint) @@ -415,12 +411,18 @@ func (p *peer) loadActiveChannels(chans []*channeldb.OpenChannel) error { } // Create the link and add it to the switch. - err = p.addLink(chanPoint, lnChan, forwardingPolicy, blockEpoch, - chainEvents, currentHeight, true) + err = p.addLink( + chanPoint, lnChan, forwardingPolicy, blockEpoch, + chainEvents, currentHeight, true, + ) if err != nil { lnChan.Stop() return err } + + p.activeChanMtx.Lock() + p.activeChannels[chanID] = lnChan + p.activeChanMtx.Unlock() } return nil