Skip to content

Commit 6bb921e

Browse files
committed
staticaddr: quit deposit fsm handler when finialized
1 parent bf3a559 commit 6bb921e

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

staticaddr/deposit/fsm.go

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

142142
blockNtfnChan chan uint32
143143

144+
// quitChan stops after the FSM stops consuming blockNtfnChan.
145+
quitChan chan struct{}
146+
147+
// spentChan is used to signal that the deposit has been spent and that
148+
// the FSM should stop processing block notifications and exit.
149+
spentChan chan struct{}
150+
151+
// finalizedDepositChan is used to signal that the deposit has been
152+
// finalized and the FSM can be removed from the manager's memory.
144153
finalizedDepositChan chan wire.OutPoint
145154
}
146155

@@ -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: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,44 @@ 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 and end the fsm's
161+
// 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+
174+
m.mu.Lock()
151175
delete(m.activeDeposits, outpoint)
176+
m.mu.Unlock()
152177

153178
case err = <-newBlockErrChan:
154179
return err
@@ -197,7 +222,9 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
197222
}
198223
}()
199224

225+
m.mu.Lock()
200226
m.activeDeposits[d.OutPoint] = fsm
227+
m.mu.Unlock()
201228
}
202229

203230
return nil

0 commit comments

Comments
 (0)