Merge pull request #7842 from morehouse/refactor_handle_warning_error

peer: combine handleWarning and handleError
This commit is contained in:
Oliver Gugger 2023-07-27 10:32:07 +02:00 committed by GitHub
commit d46ee0bd74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1564,11 +1564,11 @@ out:
case *lnwire.Warning:
targetChan = msg.ChanID
isLinkUpdate = p.handleWarning(msg)
isLinkUpdate = p.handleWarningOrError(targetChan, msg)
case *lnwire.Error:
targetChan = msg.ChanID
isLinkUpdate = p.handleError(msg)
isLinkUpdate = p.handleWarningOrError(targetChan, msg)
case *lnwire.ChannelReestablish:
targetChan = msg.ChanID
@ -1741,65 +1741,37 @@ func (p *Brontide) storeError(err error) {
)
}
// handleWarning processes a warning message read from the remote peer. The
// boolean return indicates whether the message should be delivered to a
// targeted peer or not. The message gets stored in memory as an error if we
// have open channels with the peer we received it from.
// handleWarningOrError processes a warning or error msg and returns true if
// msg should be forwarded to the associated channel link. False is returned if
// any necessary forwarding of msg was already handled by this method. If msg is
// an error from a peer with an active channel, we'll store it in memory.
//
// NOTE: This method should only be called from within the readHandler.
func (p *Brontide) handleWarning(msg *lnwire.Warning) bool {
switch {
// Connection wide messages should be forward the warning to all the
// channels with this peer.
case msg.ChanID == lnwire.ConnectionWideID:
for _, chanStream := range p.activeMsgStreams {
chanStream.AddMsg(msg)
}
func (p *Brontide) handleWarningOrError(chanID lnwire.ChannelID,
msg lnwire.Message) bool {
return false
// If the channel ID for the warning message corresponds to a pending
// channel, then the funding manager will handle the warning.
case p.cfg.FundingManager.IsPendingChannel(msg.ChanID, p):
p.cfg.FundingManager.ProcessFundingMsg(msg, p)
return false
// If not we hand the warning to the channel link for this channel.
case p.isActiveChannel(msg.ChanID):
return true
default:
return false
if errMsg, ok := msg.(*lnwire.Error); ok {
p.storeError(errMsg)
}
}
// handleError processes an error message read from the remote peer. The boolean
// returns indicates whether the message should be delivered to a targeted peer.
// It stores the error we received from the peer in memory if we have a channel
// open with the peer.
//
// NOTE: This method should only be called from within the readHandler.
func (p *Brontide) handleError(msg *lnwire.Error) bool {
// Store the error we have received.
p.storeError(msg)
switch {
// In the case of an all-zero channel ID we want to forward the error to
// all channels with this peer.
case msg.ChanID == lnwire.ConnectionWideID:
// Connection wide messages should be forwarded to all channel links
// with this peer.
case chanID == lnwire.ConnectionWideID:
for _, chanStream := range p.activeMsgStreams {
chanStream.AddMsg(msg)
}
return false
// If the channel ID for the error message corresponds to a pending
// channel, then the funding manager will handle the error.
case p.cfg.FundingManager.IsPendingChannel(msg.ChanID, p):
// If the channel ID for the message corresponds to a pending channel,
// then the funding manager will handle it.
case p.cfg.FundingManager.IsPendingChannel(chanID, p):
p.cfg.FundingManager.ProcessFundingMsg(msg, p)
return false
// If not we hand the error to the channel link for this channel.
case p.isActiveChannel(msg.ChanID):
// If not we hand the message to the channel link for this channel.
case p.isActiveChannel(chanID):
return true
default: