mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 17:55:36 +01:00
peer: handle close messages using link lifecycle hooks
This commit is contained in:
parent
70292253d1
commit
442f1dd677
1 changed files with 50 additions and 8 deletions
|
@ -3602,6 +3602,8 @@ func (p *Brontide) StartTime() time.Time {
|
||||||
// message is received from the remote peer. We'll use this message to advance
|
// message is received from the remote peer. We'll use this message to advance
|
||||||
// the chan closer state machine.
|
// the chan closer state machine.
|
||||||
func (p *Brontide) handleCloseMsg(msg *closeMsg) {
|
func (p *Brontide) handleCloseMsg(msg *closeMsg) {
|
||||||
|
link := p.fetchLinkFromKeyAndCid(msg.cid)
|
||||||
|
|
||||||
// We'll now fetch the matching closing state machine in order to continue,
|
// We'll now fetch the matching closing state machine in order to continue,
|
||||||
// or finalize the channel closure process.
|
// or finalize the channel closure process.
|
||||||
chanCloser, err := p.fetchActiveChanCloser(msg.cid)
|
chanCloser, err := p.fetchActiveChanCloser(msg.cid)
|
||||||
|
@ -3641,6 +3643,15 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
|
||||||
// We'll either continue negotiation, or halt.
|
// We'll either continue negotiation, or halt.
|
||||||
switch typed := msg.msg.(type) {
|
switch typed := msg.msg.(type) {
|
||||||
case *lnwire.Shutdown:
|
case *lnwire.Shutdown:
|
||||||
|
// Disable incoming adds immediately.
|
||||||
|
if link != nil {
|
||||||
|
err := link.DisableAdds(htlcswitch.Incoming)
|
||||||
|
if err != nil {
|
||||||
|
p.log.Warnf("incoming link adds already disabled: %v",
|
||||||
|
link.ChanID())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
oShutdown, err := chanCloser.ReceiveShutdown(*typed)
|
oShutdown, err := chanCloser.ReceiveShutdown(*typed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleErr(err)
|
handleErr(err)
|
||||||
|
@ -3648,9 +3659,26 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
oShutdown.WhenSome(func(msg lnwire.Shutdown) {
|
oShutdown.WhenSome(func(msg lnwire.Shutdown) {
|
||||||
|
// if the link is nil it means we can immediately queue
|
||||||
|
// the Shutdown message since we don't have to wait for
|
||||||
|
// commitment transaction synchronization
|
||||||
|
if link == nil {
|
||||||
|
p.queueMsg(typed, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// When we have a Shutdown to send, we defer it til the
|
||||||
|
// next time we send a CommitSig to remain spec
|
||||||
|
// compliant.
|
||||||
|
link.OnCommitOnce(htlcswitch.Outgoing, func() {
|
||||||
|
err := link.DisableAdds(htlcswitch.Outgoing)
|
||||||
|
if err != nil {
|
||||||
|
p.log.Warn(err.Error())
|
||||||
|
}
|
||||||
p.queueMsg(&msg, nil)
|
p.queueMsg(&msg, nil)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
beginNegotiation := func() {
|
||||||
oClosingSigned, err := chanCloser.BeginNegotiation()
|
oClosingSigned, err := chanCloser.BeginNegotiation()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleErr(err)
|
handleErr(err)
|
||||||
|
@ -3660,6 +3688,20 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
|
||||||
oClosingSigned.WhenSome(func(msg lnwire.ClosingSigned) {
|
oClosingSigned.WhenSome(func(msg lnwire.ClosingSigned) {
|
||||||
p.queueMsg(&msg, nil)
|
p.queueMsg(&msg, nil)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if link == nil {
|
||||||
|
beginNegotiation()
|
||||||
|
} else {
|
||||||
|
// Now we register a flush hook to advance the
|
||||||
|
// ChanCloser and possibly send out a ClosingSigned
|
||||||
|
// when the link finishes draining.
|
||||||
|
link.OnFlushedOnce(func() {
|
||||||
|
// Remove link in goroutine to prevent deadlock.
|
||||||
|
go p.cfg.Switch.RemoveLink(msg.cid)
|
||||||
|
beginNegotiation()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
case *lnwire.ClosingSigned:
|
case *lnwire.ClosingSigned:
|
||||||
oClosingSigned, err := chanCloser.ReceiveClosingSigned(*typed)
|
oClosingSigned, err := chanCloser.ReceiveClosingSigned(*typed)
|
||||||
|
|
Loading…
Add table
Reference in a new issue