@@ -19,9 +19,8 @@ import (
1919)
2020
2121const (
22- // PollInterval is the interval in which we poll for new deposits to our
23- // static address.
24- PollInterval = 10 * time .Second
22+ // PollTimeout is the timeout for polling for new deposits.
23+ PollTimeout = 10 * time .Second
2524
2625 // MinConfs is the minimum number of confirmations we require for a
2726 // deposit to be considered available for loop-ins, coop-spends and
@@ -74,16 +73,16 @@ type ManagerConfig struct {
7473type Manager struct {
7574 cfg * ManagerConfig
7675
77- // mu guards access to activeDeposits map.
76+ // mu guards access to the activeDeposits map.
7877 mu sync.Mutex
7978
8079 // activeDeposits contains all the active static address outputs.
8180 activeDeposits map [wire.OutPoint ]* FSM
8281
83- // deposits contains all the deposits that have ever been made to the
82+ // deposits contain all the deposits that have ever been made to the
8483 // static address. This field is used to store and recover deposits. It
85- // also serves as basis for reconciliation of newly detected deposits by
86- // matching them against deposits in this map that were already seen.
84+ // also serves as a basis for reconciliation of newly detected deposits
85+ // by matching them against deposits in this map that were already seen.
8786 deposits map [wire.OutPoint ]* Deposit
8887
8988 // finalizedDepositChan is a channel that receives deposits that have
@@ -119,8 +118,11 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
119118 return err
120119 }
121120
122- // Start the deposit notifier.
123- m .pollDeposits (ctx )
121+ // Initially poll for new deposits after a restart, so we catch up with
122+ // missed deposits while we were offline.
123+ timeoutCtx , cancel := context .WithTimeout (ctx , PollTimeout )
124+ m .pollDeposits (timeoutCtx )
125+ cancel ()
124126
125127 // Communicate to the caller that the address manager has completed its
126128 // initialization.
@@ -149,6 +151,13 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
149151 }
150152 }
151153
154+ // Poll for new deposits.
155+ timeoutCtx , cancel := context .WithTimeout (
156+ ctx , PollTimeout ,
157+ )
158+ m .pollDeposits (timeoutCtx )
159+ cancel ()
160+
152161 case outpoint := <- m .finalizedDepositChan :
153162 // If deposits notify us about their finalization, flush
154163 // the finalized deposit from memory.
@@ -216,25 +225,18 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
216225// pollDeposits polls new deposits to our static address and notifies the
217226// manager's event loop about them.
218227func (m * Manager ) pollDeposits (ctx context.Context ) {
219- log .Debugf ("Waiting for new static address deposits..." )
228+ log .Debugf ("Polling for new static address deposits..." )
220229
221- go func () {
222- ticker := time .NewTicker (PollInterval )
223- defer ticker .Stop ()
224- for {
225- select {
226- case <- ticker .C :
227- err := m .reconcileDeposits (ctx )
228- if err != nil {
229- log .Errorf ("unable to reconcile " +
230- "deposits: %v" , err )
231- }
230+ select {
231+ case <- ctx .Done ():
232+ log .Debugf ("Context cancelled, exiting deposit notifier..." )
232233
233- case <- ctx .Done ():
234- return
235- }
234+ default :
235+ err := m .reconcileDeposits (ctx )
236+ if err != nil {
237+ log .Errorf ("unable to reconcile deposits: %v" , err )
236238 }
237- }()
239+ }
238240}
239241
240242// reconcileDeposits fetches all spends to our static address from our lnd
0 commit comments