Skip to content

Commit 7b6319a

Browse files
committed
peer: remove context.TODOs
And move a single one to the main LND `server`.
1 parent 1174dd6 commit 7b6319a

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

peer/brontide.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -748,12 +748,12 @@ func NewBrontide(cfg Config) *Brontide {
748748

749749
// Start starts all helper goroutines the peer needs for normal operations. In
750750
// the case this peer has already been started, then this function is a noop.
751-
func (p *Brontide) Start() error {
751+
func (p *Brontide) Start(ctx context.Context) error {
752752
if atomic.AddInt32(&p.started, 1) != 1 {
753753
return nil
754754
}
755755

756-
ctx := context.TODO()
756+
ctx, _ = p.cg.Create(ctx)
757757

758758
// Once we've finished starting up the peer, we'll signal to other
759759
// goroutines that the they can move forward to tear down the peer, or
@@ -895,7 +895,7 @@ func (p *Brontide) Start() error {
895895
p.cg.WgAdd(4)
896896
go p.queueHandler()
897897
go p.writeHandler()
898-
go p.channelManager()
898+
go p.channelManager(ctx)
899899
go p.readHandler()
900900

901901
// Signal to any external processes that the peer is now active.
@@ -2871,14 +2871,32 @@ func (p *Brontide) genDeliveryScript() ([]byte, error) {
28712871
// channels maintained with the remote peer.
28722872
//
28732873
// NOTE: This method MUST be run as a goroutine.
2874-
func (p *Brontide) channelManager() {
2874+
func (p *Brontide) channelManager(ctx context.Context) {
28752875
defer p.cg.WgDone()
28762876

28772877
// reenableTimeout will fire once after the configured channel status
28782878
// interval has elapsed. This will trigger us to sign new channel
28792879
// updates and broadcast them with the "disabled" flag unset.
28802880
reenableTimeout := time.After(p.cfg.ChanActiveTimeout)
28812881

2882+
// resetChannelState is a helper that resets all active channels to
2883+
// their default state. This should be used if we are exiting.
2884+
resetChannelState := func() {
2885+
p.activeChannels.ForEach(func(_ lnwire.ChannelID,
2886+
lc *lnwallet.LightningChannel) error {
2887+
2888+
// Exit if the channel is nil as it's a pending
2889+
// channel.
2890+
if lc == nil {
2891+
return nil
2892+
}
2893+
2894+
lc.ResetState()
2895+
2896+
return nil
2897+
})
2898+
}
2899+
28822900
out:
28832901
for {
28842902
select {
@@ -2894,7 +2912,7 @@ out:
28942912
// funding workflow. We'll initialize the necessary local
28952913
// state, and notify the htlc switch of a new link.
28962914
case req := <-p.newActiveChannel:
2897-
p.handleNewActiveChannel(req)
2915+
p.handleNewActiveChannel(ctx, req)
28982916

28992917
// The funding flow for a pending channel is failed, we will
29002918
// remove it from Brontide.
@@ -2949,21 +2967,16 @@ out:
29492967
}
29502968

29512969
case <-p.cg.Done():
2952-
// As, we've been signalled to exit, we'll reset all
2953-
// our active channel back to their default state.
2954-
p.activeChannels.ForEach(func(_ lnwire.ChannelID,
2955-
lc *lnwallet.LightningChannel) error {
2956-
2957-
// Exit if the channel is nil as it's a pending
2958-
// channel.
2959-
if lc == nil {
2960-
return nil
2961-
}
2970+
// As, we've been signalled to exit, we'll reset all our
2971+
// active channel back to their default state.
2972+
resetChannelState()
29622973

2963-
lc.ResetState()
2974+
break out
29642975

2965-
return nil
2966-
})
2976+
case <-ctx.Done():
2977+
// As, we've been signalled to exit, we'll reset all our
2978+
// active channel back to their default state.
2979+
resetChannelState()
29672980

29682981
break out
29692982
}
@@ -5185,8 +5198,8 @@ func (p *Brontide) addActiveChannel(ctx context.Context,
51855198
// handleNewActiveChannel handles a `newChannelMsg` request. Depending on we
51865199
// know this channel ID or not, we'll either add it to the `activeChannels` map
51875200
// or init the next revocation for it.
5188-
func (p *Brontide) handleNewActiveChannel(req *newChannelMsg) {
5189-
ctx := context.TODO()
5201+
func (p *Brontide) handleNewActiveChannel(ctx context.Context,
5202+
req *newChannelMsg) {
51905203

51915204
newChan := req.channel
51925205
chanPoint := newChan.FundingOutpoint

peer/test_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func createTestPeerWithChannel(t *testing.T, updateChan func(a,
339339
alicePeer.activeChannels.Store(chanID, channelAlice)
340340

341341
alicePeer.cg.WgAdd(1)
342-
go alicePeer.channelManager()
342+
go alicePeer.channelManager(context.Background())
343343

344344
return &peerTestCtx{
345345
peer: alicePeer,
@@ -783,7 +783,7 @@ func startPeer(t *testing.T, mockConn *mockMessageConn,
783783
// indicates a successful startup.
784784
done := make(chan struct{})
785785
go func() {
786-
require.NoError(t, peer.Start())
786+
require.NoError(t, peer.Start(context.Background()))
787787
close(done)
788788
}()
789789

server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4577,6 +4577,8 @@ func (s *server) addPeer(p *peer.Brontide) {
45774577
//
45784578
// NOTE: This MUST be launched as a goroutine.
45794579
func (s *server) peerInitializer(p *peer.Brontide) {
4580+
ctx := context.TODO()
4581+
45804582
defer s.wg.Done()
45814583

45824584
pubBytes := p.IdentityKey().SerializeCompressed()
@@ -4604,7 +4606,7 @@ func (s *server) peerInitializer(p *peer.Brontide) {
46044606

46054607
// Start the peer! If an error occurs, we Disconnect the peer, which
46064608
// will unblock the peerTerminationWatcher.
4607-
if err := p.Start(); err != nil {
4609+
if err := p.Start(ctx); err != nil {
46084610
srvrLog.Warnf("Starting peer=%x got error: %v", pubBytes, err)
46094611

46104612
p.Disconnect(fmt.Errorf("unable to start peer: %w", err))

0 commit comments

Comments
 (0)