From f9d4212eccc1760a29ef8f1bb8c350428147bec9 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 12 Jul 2023 14:36:09 +0800 Subject: [PATCH] peer: send msgs to `chanStream` for both active and pending channels This commit now sends messages to `chanStream` for both pending and active channels. If the message is sent to a pending channel, it will be queued in `chanStream`. Once the channel link becomes active, the early messages will be processed. --- peer/brontide.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/peer/brontide.go b/peer/brontide.go index 3e84042de..43d4e57f3 100644 --- a/peer/brontide.go +++ b/peer/brontide.go @@ -1607,8 +1607,7 @@ out: case *lnwire.ChannelReestablish: targetChan = msg.ChanID - isLinkUpdate = p.isActiveChannel(targetChan) || - p.isPendingChannel(targetChan) + isLinkUpdate = p.hasChannel(targetChan) // If we failed to find the link in question, and the // message received was a channel sync message, then @@ -1625,9 +1624,22 @@ out: } } + // For messages that implement the LinkUpdater interface, we + // will consider them as link updates and send them to + // chanStream. These messages will be queued inside chanStream + // if the channel is not active yet. case LinkUpdater: targetChan = msg.TargetChanID() - isLinkUpdate = p.isActiveChannel(targetChan) + isLinkUpdate = p.hasChannel(targetChan) + + // Log an error if we don't have this channel. This + // means the peer has sent us a message with unknown + // channel ID. + if !isLinkUpdate { + p.log.Errorf("Unknown channel ID: %v found "+ + "in received msg=%s", targetChan, + nextMsg.MsgType()) + } case *lnwire.ChannelUpdate, *lnwire.ChannelAnnouncement, @@ -1729,6 +1741,13 @@ func (p *Brontide) isPendingChannel(chanID lnwire.ChannelID) bool { return channel == nil } +// hasChannel returns true if the peer has a pending/active channel specified +// by the channel ID. +func (p *Brontide) hasChannel(chanID lnwire.ChannelID) bool { + _, ok := p.activeChannels.Load(chanID) + return ok +} + // storeError stores an error in our peer's buffer of recent errors with the // current timestamp. Errors are only stored if we have at least one active // channel with the peer to mitigate a dos vector where a peer costlessly