2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-04 06:00:21 +00:00

sweepbatcher: refactor monitorSpendAndNotify to return an error

This commit is contained in:
Andras Banki-Horvath 2024-02-02 22:04:09 +01:00
parent ab30e0b4e8
commit c094ad4a85
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

View File

@ -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.
// Instead we directly detect and return the spend here.
if completed && *notifier != (SpendNotifier{}) {
go b.monitorSpendAndNotify(ctx, sweep, notifier)
return nil
return b.monitorSpendAndNotify(ctx, sweep, 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
// the response back to the response channel.
func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
notifier *SpendNotifier) {
b.wg.Add(1)
defer b.wg.Done()
notifier *SpendNotifier) error {
spendCtx, cancel := context.WithCancel(ctx)
defer cancel()
@ -522,44 +518,44 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
sweep.initiationHeight,
)
if err != nil {
select {
case notifier.SpendErrChan <- err:
case <-ctx.Done():
}
_ = b.writeToErrChan(ctx, err)
return
return err
}
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 {
select {
case spend := <-spendChan:
for {
select {
case notifier.SpendChan <- spend.SpendingTx:
case spend := <-spendChan:
select {
case notifier.SpendChan <- spend.SpendingTx:
case <-ctx.Done():
}
return
case err := <-spendErr:
select {
case notifier.SpendErrChan <- err:
case <-ctx.Done():
}
_ = b.writeToErrChan(ctx, err)
return
case <-notifier.QuitChan:
return
case <-ctx.Done():
return
}
return
case err := <-spendErr:
select {
case notifier.SpendErrChan <- err:
case <-ctx.Done():
}
_ = b.writeToErrChan(ctx, err)
return
case <-notifier.QuitChan:
return
case <-ctx.Done():
return
}
}
}()
return nil
}
func (b *Batcher) writeToErrChan(ctx context.Context, err error) error {