From f632a58b3b37a12e88ac099f0894e15396dc2954 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 24 Nov 2022 06:58:33 +0800 Subject: [PATCH] htlcswitch+peer: notify inactive link event when `htlcManager` exits --- htlcswitch/link.go | 9 ++++++++- htlcswitch/link_test.go | 40 +++++++++++++++++++++------------------- htlcswitch/switch.go | 2 ++ htlcswitch/test_utils.go | 1 + peer/brontide.go | 2 ++ 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 65bdd9977..88476f33e 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -293,6 +293,10 @@ type ChannelLinkConfig struct { // when channels become inactive. NotifyInactiveChannel func(wire.OutPoint) + // NotifyInactiveLinkEvent allows the switch to tell the + // ChannelNotifier when a channel link become inactive. + NotifyInactiveLinkEvent func(wire.OutPoint) + // HtlcNotifier is an instance of a htlcNotifier which we will pipe htlc // events through. HtlcNotifier htlcNotifier @@ -979,8 +983,11 @@ func (l *channelLink) htlcManager() { l.log.Infof("HTLC manager started, bandwidth=%v", l.Bandwidth()) // Notify any clients that the link is now in the switch via an - // ActiveLinkEvent. + // ActiveLinkEvent. We'll also defer an inactive link notification for + // when the link exits to ensure that every active notification is + // matched by an inactive one. l.cfg.NotifyActiveLink(*l.ChannelPoint()) + defer l.cfg.NotifyInactiveLinkEvent(*l.ChannelPoint()) // TODO(roasbeef): need to call wipe chan whenever D/C? diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index d0aac8551..55d77d13d 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1886,16 +1886,17 @@ func newSingleLinkTestHarness(t *testing.T, chanAmt, chanReserve btcutil.Amount) PendingCommitTicker: ticker.New(time.Minute), // Make the BatchSize and Min/MaxFeeUpdateTimeout large enough // to not trigger commit updates automatically during tests. - BatchSize: 10000, - MinFeeUpdateTimeout: 30 * time.Minute, - MaxFeeUpdateTimeout: 40 * time.Minute, - MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, - MaxFeeAllocation: DefaultMaxLinkFeeAllocation, - NotifyActiveLink: func(wire.OutPoint) {}, - NotifyActiveChannel: func(wire.OutPoint) {}, - NotifyInactiveChannel: func(wire.OutPoint) {}, - HtlcNotifier: aliceSwitch.cfg.HtlcNotifier, - GetAliases: getAliases, + BatchSize: 10000, + MinFeeUpdateTimeout: 30 * time.Minute, + MaxFeeUpdateTimeout: 40 * time.Minute, + MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, + MaxFeeAllocation: DefaultMaxLinkFeeAllocation, + NotifyActiveLink: func(wire.OutPoint) {}, + NotifyActiveChannel: func(wire.OutPoint) {}, + NotifyInactiveChannel: func(wire.OutPoint) {}, + NotifyInactiveLinkEvent: func(wire.OutPoint) {}, + HtlcNotifier: aliceSwitch.cfg.HtlcNotifier, + GetAliases: getAliases, } aliceLink := NewChannelLink(aliceCfg, aliceLc.channel) @@ -4329,15 +4330,16 @@ func (h *persistentLinkHarness) restartLink( MinFeeUpdateTimeout: 30 * time.Minute, MaxFeeUpdateTimeout: 40 * time.Minute, // Set any hodl flags requested for the new link. - HodlMask: hodl.MaskFromFlags(hodlFlags...), - MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, - MaxFeeAllocation: DefaultMaxLinkFeeAllocation, - NotifyActiveLink: func(wire.OutPoint) {}, - NotifyActiveChannel: func(wire.OutPoint) {}, - NotifyInactiveChannel: func(wire.OutPoint) {}, - HtlcNotifier: aliceSwitch.cfg.HtlcNotifier, - SyncStates: syncStates, - GetAliases: getAliases, + HodlMask: hodl.MaskFromFlags(hodlFlags...), + MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, + MaxFeeAllocation: DefaultMaxLinkFeeAllocation, + NotifyActiveLink: func(wire.OutPoint) {}, + NotifyActiveChannel: func(wire.OutPoint) {}, + NotifyInactiveChannel: func(wire.OutPoint) {}, + NotifyInactiveLinkEvent: func(wire.OutPoint) {}, + HtlcNotifier: aliceSwitch.cfg.HtlcNotifier, + SyncStates: syncStates, + GetAliases: getAliases, } aliceLink := NewChannelLink(aliceCfg, aliceChannel) diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index 460bf31b5..6f632be35 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -2293,6 +2293,8 @@ func (s *Switch) AddLink(link ChannelLink) error { link.attachFailAliasUpdate(s.failAliasUpdate) if err := link.Start(); err != nil { + log.Errorf("AddLink failed to start link with chanID=%v: %v", + chanID, err) s.removeLink(chanID) return err } diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 6e8defe3f..e1bd6783a 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -1159,6 +1159,7 @@ func (h *hopNetwork) createChannelLink(server, peer *mockServer, NotifyActiveLink: func(wire.OutPoint) {}, NotifyActiveChannel: func(wire.OutPoint) {}, NotifyInactiveChannel: func(wire.OutPoint) {}, + NotifyInactiveLinkEvent: func(wire.OutPoint) {}, HtlcNotifier: server.htlcSwitch.cfg.HtlcNotifier, GetAliases: getAliases, }, diff --git a/peer/brontide.go b/peer/brontide.go index 7ac65aaf8..6254c44d3 100644 --- a/peer/brontide.go +++ b/peer/brontide.go @@ -941,6 +941,7 @@ func (p *Brontide) addLink(chanPoint *wire.OutPoint, towerClient = p.cfg.TowerClient } + //nolint:lll linkCfg := htlcswitch.ChannelLinkConfig{ Peer: p, DecodeHopIterators: p.cfg.Sphinx.DecodeHopIterators, @@ -976,6 +977,7 @@ func (p *Brontide) addLink(chanPoint *wire.OutPoint, NotifyActiveLink: p.cfg.ChannelNotifier.NotifyActiveLinkEvent, NotifyActiveChannel: p.cfg.ChannelNotifier.NotifyActiveChannelEvent, NotifyInactiveChannel: p.cfg.ChannelNotifier.NotifyInactiveChannelEvent, + NotifyInactiveLinkEvent: p.cfg.ChannelNotifier.NotifyInactiveLinkEvent, HtlcNotifier: p.cfg.HtlcNotifier, GetAliases: p.cfg.GetAliases, }