Skip to content

Commit 5de7792

Browse files
committed
htlcswitch: ability to start link in shutdown mode
In this commit, we add the ability to start a link in shutdown mode. This means that we immediately disable any new HTLC adds in the outgoing direction and that we queue up a Shutdown message after the next CommitSig message is sent (or immediately if no CommitSig message is owed).
1 parent dc25b42 commit 5de7792

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

htlcswitch/link.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightningnetwork/lnd/channeldb"
2020
"github.com/lightningnetwork/lnd/channeldb/models"
2121
"github.com/lightningnetwork/lnd/contractcourt"
22+
"github.com/lightningnetwork/lnd/fn"
2223
"github.com/lightningnetwork/lnd/htlcswitch/hodl"
2324
"github.com/lightningnetwork/lnd/htlcswitch/hop"
2425
"github.com/lightningnetwork/lnd/invoices"
@@ -271,6 +272,14 @@ type ChannelLinkConfig struct {
271272
// GetAliases is used by the link and switch to fetch the set of
272273
// aliases for a given link.
273274
GetAliases func(base lnwire.ShortChannelID) []lnwire.ShortChannelID
275+
276+
// PreviouslySentShutdown is an optional value that is set if, at the
277+
// time of the link being started, persisted shutdown info was found for
278+
// the channel. This value being set means that we previously sent a
279+
// Shutdown message to our peer, and so we should do so again on
280+
// re-establish and should not allow anymore HTLC adds on the outgoing
281+
// direction of the link.
282+
PreviouslySentShutdown fn.Option[lnwire.Shutdown]
274283
}
275284

276285
// channelLink is the service which drives a channel's commitment update
@@ -1190,6 +1199,25 @@ func (l *channelLink) htlcManager() {
11901199
}
11911200
}
11921201

1202+
// If a shutdown message has previously been sent on this link, then we
1203+
// need to make sure that we have disabled any HTLC adds on the outgoing
1204+
// direction of the link and that we re-resend the same shutdown message
1205+
// that we previously sent.
1206+
l.cfg.PreviouslySentShutdown.WhenSome(func(shutdown lnwire.Shutdown) {
1207+
// Immediately disallow any new outgoing HTLCs.
1208+
if !l.DisableAdds(Outgoing) {
1209+
l.log.Warnf("Outgoing link adds already disabled")
1210+
}
1211+
1212+
// Re-send the shutdown message the peer. Since syncChanStates
1213+
// would have sent any outstanding CommitSig, it is fine for us
1214+
// to immediately queue the shutdown message now.
1215+
err := l.cfg.Peer.SendMessage(false, &shutdown)
1216+
if err != nil {
1217+
l.log.Warnf("Error sending shutdown message: %v", err)
1218+
}
1219+
})
1220+
11931221
// We've successfully reestablished the channel, mark it as such to
11941222
// allow the switch to forward HTLCs in the outbound direction.
11951223
l.markReestablished()

0 commit comments

Comments
 (0)