Skip to content

Commit 46ce108

Browse files
committed
netann: remove context.TODO
1 parent 32bb4ec commit 46ce108

File tree

1 file changed

+55
-38
lines changed

1 file changed

+55
-38
lines changed

netann/chan_status_manager.go

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/btcsuite/btcd/btcec/v2"
1010
"github.com/btcsuite/btcd/wire"
1111
"github.com/lightningnetwork/lnd/channeldb"
12+
"github.com/lightningnetwork/lnd/fn"
1213
graphdb "github.com/lightningnetwork/lnd/graph/db"
1314
"github.com/lightningnetwork/lnd/keychain"
1415
"github.com/lightningnetwork/lnd/lnwallet"
@@ -128,8 +129,9 @@ type ChanStatusManager struct {
128129
// become inactive.
129130
statusSampleTicker *time.Ticker
130131

131-
wg sync.WaitGroup
132-
quit chan struct{}
132+
wg sync.WaitGroup
133+
quit chan struct{}
134+
cancel fn.Option[context.CancelFunc]
133135
}
134136

135137
// NewChanStatusManager initializes a new ChanStatusManager using the given
@@ -177,20 +179,24 @@ func (m *ChanStatusManager) Start() error {
177179
var err error
178180
m.started.Do(func() {
179181
log.Info("Channel Status Manager starting")
180-
err = m.start()
182+
183+
ctx, cancel := context.WithCancel(context.Background())
184+
m.cancel = fn.Some(cancel)
185+
186+
err = m.start(ctx)
181187
})
182188
return err
183189
}
184190

185-
func (m *ChanStatusManager) start() error {
191+
func (m *ChanStatusManager) start(ctx context.Context) error {
186192
channels, err := m.fetchChannels()
187193
if err != nil {
188194
return err
189195
}
190196

191197
// Populate the initial states of all confirmed, public channels.
192198
for _, c := range channels {
193-
_, err := m.getOrInitChanStatus(c.FundingOutpoint)
199+
_, err := m.getOrInitChanStatus(ctx, c.FundingOutpoint)
194200
switch {
195201

196202
// If we can't retrieve the edge info for this channel, it may
@@ -219,7 +225,7 @@ func (m *ChanStatusManager) start() error {
219225
}
220226

221227
m.wg.Add(1)
222-
go m.statusManager()
228+
go m.statusManager(ctx)
223229

224230
return nil
225231
}
@@ -230,6 +236,10 @@ func (m *ChanStatusManager) Stop() error {
230236
log.Info("Channel Status Manager shutting down...")
231237
defer log.Debug("Channel Status Manager shutdown complete")
232238

239+
m.cancel.WhenSome(func(cancel context.CancelFunc) {
240+
cancel()
241+
})
242+
233243
close(m.quit)
234244
m.wg.Wait()
235245
})
@@ -333,23 +343,28 @@ func (m *ChanStatusManager) submitRequest(reqChan chan statusRequest,
333343
// should be scheduled or broadcast.
334344
//
335345
// NOTE: This method MUST be run as a goroutine.
336-
func (m *ChanStatusManager) statusManager() {
346+
func (m *ChanStatusManager) statusManager(ctx context.Context) {
337347
defer m.wg.Done()
338348

339349
for {
340350
select {
341351

342352
// Process any requests to mark channel as enabled.
343353
case req := <-m.enableRequests:
344-
req.errChan <- m.processEnableRequest(req.outpoint, req.manual)
354+
req.errChan <- m.processEnableRequest(
355+
ctx, req.outpoint, req.manual,
356+
)
345357

346358
// Process any requests to mark channel as disabled.
347359
case req := <-m.disableRequests:
348-
req.errChan <- m.processDisableRequest(req.outpoint, req.manual)
360+
req.errChan <- m.processDisableRequest(
361+
ctx, req.outpoint, req.manual,
362+
)
349363

350-
// Process any requests to restore automatic channel state management.
364+
// Process any requests to restore automatic channel state
365+
// management.
351366
case req := <-m.autoRequests:
352-
req.errChan <- m.processAutoRequest(req.outpoint)
367+
req.errChan <- m.processAutoRequest(ctx, req.outpoint)
353368

354369
// Use long-polling to detect when channels become inactive.
355370
case <-m.statusSampleTicker.C:
@@ -358,12 +373,12 @@ func (m *ChanStatusManager) statusManager() {
358373
// ChanStatusPendingDisabled. The channel will then be
359374
// disabled if no request to enable is received before
360375
// the ChanDisableTimeout expires.
361-
m.markPendingInactiveChannels()
376+
m.markPendingInactiveChannels(ctx)
362377

363378
// Now, do another sweep to disable any channels that
364379
// were marked in a prior iteration as pending inactive
365380
// if the inactive chan timeout has elapsed.
366-
m.disableInactiveChannels()
381+
m.disableInactiveChannels(ctx)
367382

368383
case <-m.quit:
369384
return
@@ -383,10 +398,10 @@ func (m *ChanStatusManager) statusManager() {
383398
//
384399
// An update will be broadcast only if the channel is currently disabled,
385400
// otherwise no update will be sent on the network.
386-
func (m *ChanStatusManager) processEnableRequest(outpoint wire.OutPoint,
387-
manual bool) error {
401+
func (m *ChanStatusManager) processEnableRequest(ctx context.Context,
402+
outpoint wire.OutPoint, manual bool) error {
388403

389-
curState, err := m.getOrInitChanStatus(outpoint)
404+
curState, err := m.getOrInitChanStatus(ctx, outpoint)
390405
if err != nil {
391406
return err
392407
}
@@ -423,7 +438,7 @@ func (m *ChanStatusManager) processEnableRequest(outpoint wire.OutPoint,
423438
case ChanStatusDisabled:
424439
log.Infof("Announcing channel(%v) enabled", outpoint)
425440

426-
err := m.signAndSendNextUpdate(outpoint, false)
441+
err := m.signAndSendNextUpdate(ctx, outpoint, false)
427442
if err != nil {
428443
return err
429444
}
@@ -441,10 +456,10 @@ func (m *ChanStatusManager) processEnableRequest(outpoint wire.OutPoint,
441456
//
442457
// An update will only be sent if the channel has a status other than
443458
// ChanStatusEnabled, otherwise no update will be sent on the network.
444-
func (m *ChanStatusManager) processDisableRequest(outpoint wire.OutPoint,
445-
manual bool) error {
459+
func (m *ChanStatusManager) processDisableRequest(ctx context.Context,
460+
outpoint wire.OutPoint, manual bool) error {
446461

447-
curState, err := m.getOrInitChanStatus(outpoint)
462+
curState, err := m.getOrInitChanStatus(ctx, outpoint)
448463
if err != nil {
449464
return err
450465
}
@@ -454,7 +469,7 @@ func (m *ChanStatusManager) processDisableRequest(outpoint wire.OutPoint,
454469
log.Infof("Announcing channel(%v) disabled [requested]",
455470
outpoint)
456471

457-
err := m.signAndSendNextUpdate(outpoint, true)
472+
err := m.signAndSendNextUpdate(ctx, outpoint, true)
458473
if err != nil {
459474
return err
460475
}
@@ -483,8 +498,10 @@ func (m *ChanStatusManager) processDisableRequest(outpoint wire.OutPoint,
483498
// which automatic / background requests are ignored).
484499
//
485500
// No update will be sent on the network.
486-
func (m *ChanStatusManager) processAutoRequest(outpoint wire.OutPoint) error {
487-
curState, err := m.getOrInitChanStatus(outpoint)
501+
func (m *ChanStatusManager) processAutoRequest(ctx context.Context,
502+
outpoint wire.OutPoint) error {
503+
504+
curState, err := m.getOrInitChanStatus(ctx, outpoint)
488505
if err != nil {
489506
return err
490507
}
@@ -505,7 +522,7 @@ func (m *ChanStatusManager) processAutoRequest(outpoint wire.OutPoint) error {
505522
// request to enable is received before the scheduled disable is broadcast, or
506523
// the channel is successfully re-enabled and channel is returned to an active
507524
// state from the POV of the ChanStatusManager.
508-
func (m *ChanStatusManager) markPendingInactiveChannels() {
525+
func (m *ChanStatusManager) markPendingInactiveChannels(ctx context.Context) {
509526
channels, err := m.fetchChannels()
510527
if err != nil {
511528
log.Errorf("Unable to load active channels: %v", err)
@@ -515,7 +532,7 @@ func (m *ChanStatusManager) markPendingInactiveChannels() {
515532
for _, c := range channels {
516533
// Determine the initial status of the active channel, and
517534
// populate the entry in the chanStates map.
518-
curState, err := m.getOrInitChanStatus(c.FundingOutpoint)
535+
curState, err := m.getOrInitChanStatus(ctx, c.FundingOutpoint)
519536
if err != nil {
520537
log.Errorf("Unable to retrieve chan status for "+
521538
"Channel(%v): %v", c.FundingOutpoint, err)
@@ -554,7 +571,7 @@ func (m *ChanStatusManager) markPendingInactiveChannels() {
554571
// disableInactiveChannels scans through the set of monitored channels, and
555572
// broadcast a disable update for any pending inactive channels whose
556573
// SendDisableTime has been superseded by the current time.
557-
func (m *ChanStatusManager) disableInactiveChannels() {
574+
func (m *ChanStatusManager) disableInactiveChannels(ctx context.Context) {
558575
// Now, disable any channels whose inactive chan timeout has elapsed.
559576
now := time.Now()
560577
for outpoint, state := range m.chanStates {
@@ -573,7 +590,7 @@ func (m *ChanStatusManager) disableInactiveChannels() {
573590
"[detected]", outpoint)
574591

575592
// Sign an update disabling the channel.
576-
err := m.signAndSendNextUpdate(outpoint, true)
593+
err := m.signAndSendNextUpdate(ctx, outpoint, true)
577594
if err != nil {
578595
log.Errorf("Unable to sign update disabling "+
579596
"channel(%v): %v", outpoint, err)
@@ -626,12 +643,14 @@ func (m *ChanStatusManager) fetchChannels() ([]*channeldb.OpenChannel, error) {
626643
// use the current time as the update's timestamp, or increment the old
627644
// timestamp by 1 to ensure the update can propagate. If signing is successful,
628645
// the new update will be sent out on the network.
629-
func (m *ChanStatusManager) signAndSendNextUpdate(outpoint wire.OutPoint,
630-
disabled bool) error {
646+
func (m *ChanStatusManager) signAndSendNextUpdate(ctx context.Context,
647+
outpoint wire.OutPoint, disabled bool) error {
631648

632649
// Retrieve the latest update for this channel. We'll use this
633650
// as our starting point to send the new update.
634-
chanUpdate, private, err := m.fetchLastChanUpdateByOutPoint(outpoint)
651+
chanUpdate, private, err := m.fetchLastChanUpdateByOutPoint(
652+
ctx, outpoint,
653+
)
635654
if err != nil {
636655
return err
637656
}
@@ -651,10 +670,8 @@ func (m *ChanStatusManager) signAndSendNextUpdate(outpoint wire.OutPoint,
651670
// a channel, and crafts a new ChannelUpdate with this policy. Returns an error
652671
// in case our ChannelEdgePolicy is not found in the database. Also returns if
653672
// the channel is private by checking AuthProof for nil.
654-
func (m *ChanStatusManager) fetchLastChanUpdateByOutPoint(op wire.OutPoint) (
655-
*lnwire.ChannelUpdate1, bool, error) {
656-
657-
ctx := context.TODO()
673+
func (m *ChanStatusManager) fetchLastChanUpdateByOutPoint(ctx context.Context,
674+
op wire.OutPoint) (*lnwire.ChannelUpdate1, bool, error) {
658675

659676
// Get the edge info and policies for this channel from the graph.
660677
info, edge1, edge2, err := m.cfg.Graph.FetchChannelEdgesByOutpoint(
@@ -675,10 +692,10 @@ func (m *ChanStatusManager) fetchLastChanUpdateByOutPoint(op wire.OutPoint) (
675692
// ChanStatusEnabled or ChanStatusDisabled, determined by inspecting the bits on
676693
// the most recent announcement. An error is returned if the latest update could
677694
// not be retrieved.
678-
func (m *ChanStatusManager) loadInitialChanState(
695+
func (m *ChanStatusManager) loadInitialChanState(ctx context.Context,
679696
outpoint *wire.OutPoint) (ChannelState, error) {
680697

681-
lastUpdate, _, err := m.fetchLastChanUpdateByOutPoint(*outpoint)
698+
lastUpdate, _, err := m.fetchLastChanUpdateByOutPoint(ctx, *outpoint)
682699
if err != nil {
683700
return ChannelState{}, err
684701
}
@@ -701,7 +718,7 @@ func (m *ChanStatusManager) loadInitialChanState(
701718
// outpoint. If the chanStates map already contains an entry for the outpoint,
702719
// the value in the map is returned. Otherwise, the outpoint's initial status is
703720
// computed and updated in the chanStates map before being returned.
704-
func (m *ChanStatusManager) getOrInitChanStatus(
721+
func (m *ChanStatusManager) getOrInitChanStatus(ctx context.Context,
705722
outpoint wire.OutPoint) (ChannelState, error) {
706723

707724
// Return the current ChannelState from the chanStates map if it is
@@ -712,7 +729,7 @@ func (m *ChanStatusManager) getOrInitChanStatus(
712729

713730
// Otherwise, determine the initial state based on the last update we
714731
// sent for the outpoint.
715-
initialState, err := m.loadInitialChanState(&outpoint)
732+
initialState, err := m.loadInitialChanState(ctx, &outpoint)
716733
if err != nil {
717734
return ChannelState{}, err
718735
}

0 commit comments

Comments
 (0)