Skip to content

Commit 1f8f282

Browse files
authored
Merge pull request #1003 from hieblmi/recon-on-block
staticaddr: poll deposits on block arrival
2 parents cef0fae + b36f165 commit 1f8f282

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

staticaddr/deposit/manager.go

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ import (
1919
)
2020

2121
const (
22-
// PollInterval is the interval in which we poll for new deposits to our
23-
// static address.
24-
PollInterval = 10 * time.Second
25-
2622
// MinConfs is the minimum number of confirmations we require for a
2723
// deposit to be considered available for loop-ins, coop-spends and
2824
// timeouts.
@@ -74,16 +70,16 @@ type ManagerConfig struct {
7470
type Manager struct {
7571
cfg *ManagerConfig
7672

77-
// mu guards access to activeDeposits map.
73+
// mu guards access to the activeDeposits map.
7874
mu sync.Mutex
7975

8076
// activeDeposits contains all the active static address outputs.
8177
activeDeposits map[wire.OutPoint]*FSM
8278

83-
// deposits contains all the deposits that have ever been made to the
79+
// deposits contain all the deposits that have ever been made to the
8480
// 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.
81+
// also serves as a basis for reconciliation of newly detected deposits
82+
// by matching them against deposits in this map that were already seen.
8783
deposits map[wire.OutPoint]*Deposit
8884

8985
// finalizedDepositChan is a channel that receives deposits that have
@@ -119,8 +115,13 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
119115
return err
120116
}
121117

122-
// Start the deposit notifier.
123-
m.pollDeposits(ctx)
118+
// Initially reconcile new deposits after a restart, so we catch up with
119+
// missed deposits while we were offline.
120+
if err = m.reconcileDeposits(ctx); err != nil {
121+
log.Errorf("unable to reconcile deposits: %v", err)
122+
123+
return err
124+
}
124125

125126
// Communicate to the caller that the address manager has completed its
126127
// initialization.
@@ -149,6 +150,15 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
149150
}
150151
}
151152

153+
// Reconcile new deposits that might have just gotten
154+
// confirmed.
155+
if err = m.reconcileDeposits(ctx); err != nil {
156+
log.Errorf("unable to reconcile deposits: %v",
157+
err)
158+
159+
return err
160+
}
161+
152162
case outpoint := <-m.finalizedDepositChan:
153163
// If deposits notify us about their finalization, flush
154164
// the finalized deposit from memory.
@@ -213,31 +223,7 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
213223
return nil
214224
}
215225

216-
// pollDeposits polls new deposits to our static address and notifies the
217-
// manager's event loop about them.
218-
func (m *Manager) pollDeposits(ctx context.Context) {
219-
log.Debugf("Waiting for new static address deposits...")
220-
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-
}
232-
233-
case <-ctx.Done():
234-
return
235-
}
236-
}
237-
}()
238-
}
239-
240-
// reconcileDeposits fetches all spends to our static address from our lnd
226+
// reconcileDeposits fetches all spends to our static addresses from our lnd
241227
// wallet and matches it against the deposits in our memory that we've seen so
242228
// far. It picks the newly identified deposits and starts a state machine per
243229
// deposit to track its progress.

0 commit comments

Comments
 (0)