sweepbatcher: refactor monitorSpendAndNotify to return an error

pull/694/head
Andras Banki-Horvath 4 months ago
parent ab30e0b4e8
commit c094ad4a85
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

@ -270,8 +270,7 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
// can't attach its notifier to the batch as that is no longer running. // can't attach its notifier to the batch as that is no longer running.
// Instead we directly detect and return the spend here. // Instead we directly detect and return the spend here.
if completed && *notifier != (SpendNotifier{}) { if completed && *notifier != (SpendNotifier{}) {
go b.monitorSpendAndNotify(ctx, sweep, notifier) return b.monitorSpendAndNotify(ctx, sweep, notifier)
return nil
} }
sweep.notifier = notifier sweep.notifier = notifier
@ -509,10 +508,7 @@ func (b *Batcher) FetchUnconfirmedBatches(ctx context.Context) ([]*batch,
// monitorSpendAndNotify monitors the spend of a specific outpoint and writes // monitorSpendAndNotify monitors the spend of a specific outpoint and writes
// the response back to the response channel. // the response back to the response channel.
func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep, func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
notifier *SpendNotifier) { notifier *SpendNotifier) error {
b.wg.Add(1)
defer b.wg.Done()
spendCtx, cancel := context.WithCancel(ctx) spendCtx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
@ -522,44 +518,44 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
sweep.initiationHeight, sweep.initiationHeight,
) )
if err != nil { if err != nil {
select { return err
case notifier.SpendErrChan <- err:
case <-ctx.Done():
}
_ = b.writeToErrChan(ctx, err)
return
} }
log.Infof("Batcher monitoring spend for swap %x", sweep.swapHash[:6]) b.wg.Add(1)
go func() {
defer b.wg.Done()
log.Infof("Batcher monitoring spend for swap %x",
sweep.swapHash[:6])
for { for {
select {
case spend := <-spendChan:
select { select {
case notifier.SpendChan <- spend.SpendingTx: case spend := <-spendChan:
case <-ctx.Done(): select {
} case notifier.SpendChan <- spend.SpendingTx:
case <-ctx.Done():
}
return return
case err := <-spendErr: case err := <-spendErr:
select { select {
case notifier.SpendErrChan <- err: case notifier.SpendErrChan <- err:
case <-ctx.Done(): case <-ctx.Done():
} }
_ = b.writeToErrChan(ctx, err) _ = b.writeToErrChan(ctx, err)
return return
case <-notifier.QuitChan: case <-notifier.QuitChan:
return return
case <-ctx.Done(): case <-ctx.Done():
return return
}
} }
} }()
return nil
} }
func (b *Batcher) writeToErrChan(ctx context.Context, err error) error { func (b *Batcher) writeToErrChan(ctx context.Context, err error) error {

Loading…
Cancel
Save