@@ -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+ defer cancel ()
125+ m .pollDeposits (timeoutCtx )
124126
125127 // Communicate to the caller that the address manager has completed its
126128 // initialization.
@@ -149,6 +151,16 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
149151 }
150152 }
151153
154+ // Poll for new deposits.
155+ func () {
156+ timeoutCtx , cancel = context .WithTimeout (
157+ ctx , PollTimeout ,
158+ )
159+ defer cancel ()
160+
161+ m .pollDeposits (timeoutCtx )
162+ }()
163+
152164 case outpoint := <- m .finalizedDepositChan :
153165 // If deposits notify us about their finalization, flush
154166 // the finalized deposit from memory.
@@ -213,28 +225,30 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
213225 return nil
214226}
215227
228+ // pollDepositsWithTimeout creates a timeout context and polls for new deposits.
229+ func (m * Manager ) pollDepositsWithTimeout (ctx context.Context ) error {
230+ timeoutCtx , cancel := context .WithTimeout (ctx , PollTimeout )
231+ defer cancel ()
232+
233+ m .pollDeposits (timeoutCtx )
234+ return nil
235+ }
236+
216237// pollDeposits polls new deposits to our static address and notifies the
217238// manager's event loop about them.
218239func (m * Manager ) pollDeposits (ctx context.Context ) {
219- log .Debugf ("Waiting for new static address deposits..." )
240+ log .Debugf ("Polling for new static address deposits..." )
220241
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- }
242+ select {
243+ case <- ctx .Done ():
244+ log .Debugf ("Context cancelled, exiting deposit notifier..." )
232245
233- case <- ctx .Done ():
234- return
235- }
246+ default :
247+ err := m .reconcileDeposits (ctx )
248+ if err != nil {
249+ log .Errorf ("unable to reconcile deposits: %v" , err )
236250 }
237- }()
251+ }
238252}
239253
240254// reconcileDeposits fetches all spends to our static address from our lnd
0 commit comments