Skip to content

Commit c50edea

Browse files
committed
staticaddr: lock deposit map
1 parent 7407a61 commit c50edea

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

staticaddr/deposit/manager.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ type ManagerConfig struct {
7474
type Manager struct {
7575
cfg *ManagerConfig
7676

77-
// mu guards access to activeDeposits map.
78-
mu sync.Mutex
77+
// mu guards access to the activeDeposits map.
78+
muActiveDeposits sync.Mutex
79+
80+
// mu guards access to the deposits map.
81+
muDeposits sync.Mutex
7982

8083
// initChan signals the daemon that the address manager has completed
8184
// its initialization.
@@ -136,12 +139,12 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
136139
select {
137140
case height := <-newBlockChan:
138141
// Inform all active deposits about a new block arrival.
139-
m.mu.Lock()
142+
m.muActiveDeposits.Lock()
140143
activeDeposits := make([]*FSM, 0, len(m.activeDeposits))
141144
for _, fsm := range m.activeDeposits {
142145
activeDeposits = append(activeDeposits, fsm)
143146
}
144-
m.mu.Unlock()
147+
m.muActiveDeposits.Unlock()
145148

146149
for _, fsm := range activeDeposits {
147150
select {
@@ -159,9 +162,9 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
159162
// If deposits notify us about their finalization,
160163
// flush the finalized deposit from memory and end the
161164
// fsm's block notif goroutine.
162-
m.mu.Lock()
165+
m.muActiveDeposits.Lock()
163166
fsm := m.activeDeposits[outpoint]
164-
m.mu.Unlock()
167+
m.muActiveDeposits.Unlock()
165168

166169
// End the fsm's block notif go routine.
167170
select {
@@ -171,9 +174,9 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
171174
return ctx.Err()
172175
}
173176

174-
m.mu.Lock()
177+
m.muActiveDeposits.Lock()
175178
delete(m.activeDeposits, outpoint)
176-
m.mu.Unlock()
179+
m.muActiveDeposits.Unlock()
177180

178181
case err = <-newBlockErrChan:
179182
return err
@@ -222,9 +225,9 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
222225
}
223226
}()
224227

225-
m.mu.Lock()
228+
m.muActiveDeposits.Lock()
226229
m.activeDeposits[d.OutPoint] = fsm
227-
m.mu.Unlock()
230+
m.muActiveDeposits.Unlock()
228231
}
229232

230233
return nil
@@ -344,9 +347,9 @@ func (m *Manager) createNewDeposit(ctx context.Context,
344347
return nil, err
345348
}
346349

347-
m.mu.Lock()
350+
m.muDeposits.Lock()
348351
m.deposits[deposit.OutPoint] = deposit
349-
m.mu.Unlock()
352+
m.muDeposits.Unlock()
350353

351354
return deposit, nil
352355
}
@@ -387,8 +390,8 @@ func (m *Manager) getBlockHeight(ctx context.Context,
387390
// filterNewDeposits filters the given utxos for new deposits that we haven't
388391
// seen before.
389392
func (m *Manager) filterNewDeposits(utxos []*lnwallet.Utxo) []*lnwallet.Utxo {
390-
m.mu.Lock()
391-
defer m.mu.Unlock()
393+
m.muDeposits.Lock()
394+
defer m.muDeposits.Unlock()
392395

393396
var newDeposits []*lnwallet.Utxo
394397
for _, utxo := range utxos {
@@ -424,9 +427,9 @@ func (m *Manager) startDepositFsm(ctx context.Context, deposit *Deposit) error {
424427
}
425428

426429
// Add the FSM to the active FSMs map.
427-
m.mu.Lock()
430+
m.muActiveDeposits.Lock()
428431
m.activeDeposits[deposit.OutPoint] = fsm
429-
m.mu.Unlock()
432+
m.muActiveDeposits.Unlock()
430433

431434
return nil
432435
}
@@ -437,8 +440,8 @@ func (m *Manager) startDepositFsm(ctx context.Context, deposit *Deposit) error {
437440
func (m *Manager) GetActiveDepositsInState(stateFilter fsm.StateType) (
438441
[]*Deposit, error) {
439442

440-
m.mu.Lock()
441-
defer m.mu.Unlock()
443+
m.muActiveDeposits.Lock()
444+
defer m.muActiveDeposits.Unlock()
442445

443446
var deposits []*Deposit
444447
for _, fsm := range m.activeDeposits {
@@ -472,8 +475,8 @@ func (m *Manager) GetActiveDepositsInState(stateFilter fsm.StateType) (
472475
func (m *Manager) AllOutpointsActiveDeposits(outpoints []wire.OutPoint,
473476
targetState fsm.StateType) ([]*Deposit, bool) {
474477

475-
m.mu.Lock()
476-
defer m.mu.Unlock()
478+
m.muActiveDeposits.Lock()
479+
defer m.muActiveDeposits.Unlock()
477480

478481
_, deposits := m.toActiveDeposits(&outpoints)
479482
if deposits == nil {
@@ -531,10 +534,10 @@ func (m *Manager) TransitionDeposits(ctx context.Context, deposits []*Deposit,
531534
outpoints[i] = d.OutPoint
532535
}
533536

534-
m.mu.Lock()
535-
defer m.mu.Unlock()
536-
537+
m.muActiveDeposits.Lock()
537538
stateMachines, _ := m.toActiveDeposits(&outpoints)
539+
m.muActiveDeposits.Unlock()
540+
538541
if stateMachines == nil {
539542
return fmt.Errorf("deposits not found in active deposits")
540543
}

0 commit comments

Comments
 (0)