From 6dca07577d8a1925491587217bcbccb8764014f4 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 19 Sep 2019 12:46:44 -0700 Subject: [PATCH] multi: move active/inactive ntfns from switch to link Since we will now wait to deliver the event after channel reestablish, notifying when the link is added to the switch will no longer be sufficient. Later, we will add receiving reestablish as an additional requirement for EligibleToForward returning true. The inactive ntfn is also moved, to ensure that we don't fire inactive notifications if no corresponding active notification was sent. --- htlcswitch/link.go | 16 ++++++++++++++++ htlcswitch/link_test.go | 4 ++++ htlcswitch/mock.go | 10 ++++------ htlcswitch/switch.go | 13 ------------- htlcswitch/test_utils.go | 2 ++ peer.go | 2 ++ server.go | 2 -- 7 files changed, 28 insertions(+), 21 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index c904aa3e..5ad55bc7 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -262,6 +262,14 @@ type ChannelLinkConfig struct { // commitment fee to be of its balance. This only applies to the // initiator of the channel. MaxFeeAllocation float64 + + // NotifyActiveChannel allows the link to tell the ChannelNotifier when + // channels becomes active. + NotifyActiveChannel func(wire.OutPoint) + + // NotifyInactiveChannel allows the switch to tell the ChannelNotifier + // when channels become inactive. + NotifyInactiveChannel func(wire.OutPoint) } // channelLink is the service which drives a channel's commitment update @@ -870,6 +878,14 @@ func (l *channelLink) htlcManager() { log.Infof("HTLC manager for ChannelPoint(%v) started, "+ "bandwidth=%v", l.channel.ChannelPoint(), l.Bandwidth()) + // Funding locked has already been received, so we'll go ahead and + // deliver the active channel notification since EligibleToForward + // returns true now that the link has been added to the switch. We'll + // also defer the inactive notification for when the link exits to + // ensure that every active notification is matched by an inactive one. + l.cfg.NotifyActiveChannel(*l.ChannelPoint()) + defer l.cfg.NotifyInactiveChannel(*l.ChannelPoint()) + // TODO(roasbeef): need to call wipe chan whenever D/C? // If this isn't the first time that this channel link has been diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 722c785c..e7a18751 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1688,6 +1688,8 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) ( MaxFeeUpdateTimeout: 40 * time.Minute, MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, MaxFeeAllocation: DefaultMaxLinkFeeAllocation, + NotifyActiveChannel: func(wire.OutPoint) {}, + NotifyInactiveChannel: func(wire.OutPoint) {}, } aliceLink := NewChannelLink(aliceCfg, aliceLc.channel) @@ -4249,6 +4251,8 @@ func (h *persistentLinkHarness) restartLink( HodlMask: hodl.MaskFromFlags(hodlFlags...), MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, MaxFeeAllocation: DefaultMaxLinkFeeAllocation, + NotifyActiveChannel: func(wire.OutPoint) {}, + NotifyInactiveChannel: func(wire.OutPoint) {}, } aliceLink := NewChannelLink(aliceCfg, aliceChannel) diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 98f3596e..a3174b8f 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -171,12 +171,10 @@ func initSwitchWithDB(startingHeight uint32, db *channeldb.DB) (*Switch, error) FetchLastChannelUpdate: func(lnwire.ShortChannelID) (*lnwire.ChannelUpdate, error) { return nil, nil }, - Notifier: &mockNotifier{}, - FwdEventTicker: ticker.NewForce(DefaultFwdEventInterval), - LogEventTicker: ticker.NewForce(DefaultLogInterval), - AckEventTicker: ticker.NewForce(DefaultAckInterval), - NotifyActiveChannel: func(wire.OutPoint) {}, - NotifyInactiveChannel: func(wire.OutPoint) {}, + Notifier: &mockNotifier{}, + FwdEventTicker: ticker.NewForce(DefaultFwdEventInterval), + LogEventTicker: ticker.NewForce(DefaultLogInterval), + AckEventTicker: ticker.NewForce(DefaultAckInterval), } return New(cfg, startingHeight) diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index c11c707e..0364b0c4 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -163,11 +163,6 @@ type Config struct { // fails in forwarding packages. AckEventTicker ticker.Ticker - // NotifyActiveChannel and NotifyInactiveChannel allow the link to tell - // the ChannelNotifier when channels become active and inactive. - NotifyActiveChannel func(wire.OutPoint) - NotifyInactiveChannel func(wire.OutPoint) - // RejectHTLC is a flag that instructs the htlcswitch to reject any // HTLCs that are not from the source hop. RejectHTLC bool @@ -2012,11 +2007,6 @@ func (s *Switch) addLiveLink(link ChannelLink) { s.interfaceIndex[peerPub] = make(map[lnwire.ChannelID]ChannelLink) } s.interfaceIndex[peerPub][link.ChanID()] = link - - // Inform the channel notifier if the link has become active. - if link.EligibleToForward() { - s.cfg.NotifyActiveChannel(*link.ChannelPoint()) - } } // GetLink is used to initiate the handling of the get link command. The @@ -2092,9 +2082,6 @@ func (s *Switch) removeLink(chanID lnwire.ChannelID) ChannelLink { return nil } - // Inform the Channel Notifier about the link becoming inactive. - s.cfg.NotifyInactiveChannel(*link.ChannelPoint()) - // Remove the channel from live link indexes. delete(s.pendingLinkIndex, link.ChanID()) delete(s.linkIndex, link.ChanID()) diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index cdb0aebb..7bd46531 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -1122,6 +1122,8 @@ func (h *hopNetwork) createChannelLink(server, peer *mockServer, OutgoingCltvRejectDelta: 3, MaxOutgoingCltvExpiry: DefaultMaxOutgoingCltvExpiry, MaxFeeAllocation: DefaultMaxLinkFeeAllocation, + NotifyActiveChannel: func(wire.OutPoint) {}, + NotifyInactiveChannel: func(wire.OutPoint) {}, }, channel, ) diff --git a/peer.go b/peer.go index 77161ce6..2b4460a4 100644 --- a/peer.go +++ b/peer.go @@ -576,6 +576,8 @@ func (p *peer) addLink(chanPoint *wire.OutPoint, TowerClient: p.server.towerClient, MaxOutgoingCltvExpiry: cfg.MaxOutgoingCltvExpiry, MaxFeeAllocation: cfg.MaxChannelFeeAllocation, + NotifyActiveChannel: p.server.channelNotifier.NotifyActiveChannelEvent, + NotifyInactiveChannel: p.server.channelNotifier.NotifyInactiveChannelEvent, } link := htlcswitch.NewChannelLink(linkCfg, lnChan) diff --git a/server.go b/server.go index a002aab9..f90dcd97 100644 --- a/server.go +++ b/server.go @@ -430,8 +430,6 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, FwdEventTicker: ticker.New(htlcswitch.DefaultFwdEventInterval), LogEventTicker: ticker.New(htlcswitch.DefaultLogInterval), AckEventTicker: ticker.New(htlcswitch.DefaultAckInterval), - NotifyActiveChannel: s.channelNotifier.NotifyActiveChannelEvent, - NotifyInactiveChannel: s.channelNotifier.NotifyInactiveChannelEvent, RejectHTLC: cfg.RejectHTLC, }, uint32(currentHeight)) if err != nil {