Skip to content

Commit f29cbc7

Browse files
committed
staticaddr: quit deposit fsm handler when finialized
1 parent b31979b commit f29cbc7

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

staticaddr/deposit/fsm.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,16 @@ 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
150+
151+
// spentChan is used to signal that the FSM should stop processing
152+
// block notifications and exit.
153+
spentChan chan struct{}
145154
}
146155

147156
// NewFSM creates a new state machine that can action on all static address
@@ -167,7 +176,9 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
167176
params: params,
168177
address: address,
169178
blockNtfnChan: make(chan uint32),
179+
quitChan: make(chan struct{}),
170180
finalizedDepositChan: finalizedDepositChan,
181+
spentChan: make(chan struct{}),
171182
}
172183

173184
depositStates := depoFsm.DepositStatesV0()
@@ -191,19 +202,24 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
191202

192203
depoFsm.ActionEntryFunc = depoFsm.updateDeposit
193204

194-
go func() {
205+
go func(fsm *FSM) {
206+
defer close(fsm.quitChan)
207+
195208
for {
196209
select {
197-
case currentHeight := <-depoFsm.blockNtfnChan:
210+
case currentHeight := <-fsm.blockNtfnChan:
198211
depoFsm.handleBlockNotification(
199212
ctx, currentHeight,
200213
)
201214

215+
case <-fsm.spentChan:
216+
return
217+
202218
case <-ctx.Done():
203219
return
204220
}
205221
}
206-
}()
222+
}(depoFsm)
207223

208224
return depoFsm, nil
209225
}

staticaddr/deposit/manager.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,40 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
137137
case height := <-newBlockChan:
138138
// Inform all active deposits about a new block arrival.
139139
m.mu.Lock()
140+
activeDeposits := make([]*FSM, 0, len(m.activeDeposits))
140141
for _, fsm := range m.activeDeposits {
142+
activeDeposits = append(activeDeposits, fsm)
143+
}
144+
m.mu.Unlock()
145+
146+
for _, fsm := range activeDeposits {
141147
select {
142148
case fsm.blockNtfnChan <- uint32(height):
143149

150+
case <-fsm.quitChan:
151+
continue
152+
144153
case <-ctx.Done():
145154
return ctx.Err()
146155
}
147156
}
148-
m.mu.Unlock()
157+
149158
case outpoint := <-m.finalizedDepositChan:
150-
// If deposits notify us about their finalization, we
151-
// update the manager's internal state and flush the
152-
// finalized deposit from memory.
159+
// If deposits notify us about their finalization,
160+
// flush the finalized deposit from memory and end the
161+
// fsm's block notif goroutine.
162+
m.mu.Lock()
163+
fsm := m.activeDeposits[outpoint]
164+
m.mu.Unlock()
165+
166+
// End the fsm's block notif go routine.
167+
select {
168+
case fsm.spentChan <- struct{}{}:
169+
170+
case <-ctx.Done():
171+
return ctx.Err()
172+
}
173+
153174
m.mu.Lock()
154175
delete(m.activeDeposits, outpoint)
155176
m.mu.Unlock()

0 commit comments

Comments
 (0)