Skip to content

Commit 484681a

Browse files
committed
staticaddr: quit deposit fsm handler when finialized
1 parent bf3a559 commit 484681a

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

staticaddr/deposit/fsm.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ type FSM struct {
141141

142142
blockNtfnChan chan uint32
143143

144+
// quitChan stops after the FSM stops consuming blockNtfnChan.
145+
quitChan chan struct{}
146+
147+
// finalizedDepositChan is used to signal that the deposit has been
148+
// finalized and the FSM can be removed from the manager's memory.
144149
finalizedDepositChan chan wire.OutPoint
145150
}
146151

@@ -167,6 +172,7 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
167172
params: params,
168173
address: address,
169174
blockNtfnChan: make(chan uint32),
175+
quitChan: make(chan struct{}),
170176
finalizedDepositChan: finalizedDepositChan,
171177
}
172178

@@ -191,10 +197,12 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
191197

192198
depoFsm.ActionEntryFunc = depoFsm.updateDeposit
193199

194-
go func() {
200+
go func(fsm *FSM) {
201+
defer close(fsm.quitChan)
202+
195203
for {
196204
select {
197-
case currentHeight := <-depoFsm.blockNtfnChan:
205+
case currentHeight := <-fsm.blockNtfnChan:
198206
depoFsm.handleBlockNotification(
199207
ctx, currentHeight,
200208
)
@@ -203,7 +211,7 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
203211
return
204212
}
205213
}
206-
}()
214+
}(depoFsm)
207215

208216
return depoFsm, nil
209217
}

staticaddr/deposit/manager.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,31 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
136136
select {
137137
case height := <-newBlockChan:
138138
// Inform all active deposits about a new block arrival.
139+
m.mu.Lock()
140+
activeDeposits := make([]*FSM, 0, len(m.activeDeposits))
139141
for _, fsm := range m.activeDeposits {
142+
activeDeposits = append(activeDeposits, fsm)
143+
}
144+
m.mu.Unlock()
145+
146+
for _, fsm := range activeDeposits {
140147
select {
141148
case fsm.blockNtfnChan <- uint32(height):
142149

150+
case <-fsm.quitChan:
151+
continue
152+
143153
case <-ctx.Done():
144154
return ctx.Err()
145155
}
146156
}
157+
147158
case outpoint := <-m.finalizedDepositChan:
148-
// If deposits notify us about their finalization, we
149-
// update the manager's internal state and flush the
150-
// finalized deposit from memory.
159+
// If deposits notify us about their finalization, flush
160+
// the finalized deposit from memory.
161+
m.mu.Lock()
151162
delete(m.activeDeposits, outpoint)
163+
m.mu.Unlock()
152164

153165
case err = <-newBlockErrChan:
154166
return err
@@ -197,7 +209,9 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
197209
}
198210
}()
199211

212+
m.mu.Lock()
200213
m.activeDeposits[d.OutPoint] = fsm
214+
m.mu.Unlock()
201215
}
202216

203217
return nil

0 commit comments

Comments
 (0)