diff --git a/sweepbatcher/sweep_batch.go b/sweepbatcher/sweep_batch.go index 15e7d006..ee623659 100644 --- a/sweepbatcher/sweep_batch.go +++ b/sweepbatcher/sweep_batch.go @@ -1822,29 +1822,22 @@ func (b *batch) monitorSpend(ctx context.Context, primarySweep sweep) error { b.Infof("monitoring spend for outpoint %s", primarySweep.outpoint.String()) - for { + select { + case spend := <-spendChan: select { - case spend := <-spendChan: - select { - case b.spendChan <- spend: - - case <-ctx.Done(): - } - - return - - case err := <-spendErr: - b.writeToSpendErrChan(ctx, err) - - b.writeToErrChan( - fmt.Errorf("spend error: %w", err), - ) - - return + case b.spendChan <- spend: case <-ctx.Done(): - return } + + case err := <-spendErr: + b.writeToSpendErrChan(ctx, err) + + b.writeToErrChan( + fmt.Errorf("spend error: %w", err), + ) + + case <-ctx.Done(): } }() @@ -1878,39 +1871,31 @@ func (b *batch) monitorConfirmations(ctx context.Context) error { defer cancel() defer b.wg.Done() - for { + select { + case conf := <-confChan: select { - case conf := <-confChan: - select { - case b.confChan <- conf: - - case <-ctx.Done(): - } - - return - - case err := <-errChan: - b.writeToErrChan(fmt.Errorf("confirmations "+ - "monitoring error: %w", err)) - - return - - case <-reorgChan: - // A re-org has been detected. We set the batch - // state back to open since our batch - // transaction is no longer present in any - // block. We can accept more sweeps and try to - // publish new transactions, at this point we - // need to monitor again for a new spend. - select { - case b.reorgChan <- struct{}{}: - case <-ctx.Done(): - } - return + case b.confChan <- conf: case <-ctx.Done(): - return } + + case err := <-errChan: + b.writeToErrChan(fmt.Errorf("confirmations "+ + "monitoring error: %w", err)) + + case <-reorgChan: + // A re-org has been detected. We set the batch + // state back to open since our batch + // transaction is no longer present in any + // block. We can accept more sweeps and try to + // publish new transactions, at this point we + // need to monitor again for a new spend. + select { + case b.reorgChan <- struct{}{}: + case <-ctx.Done(): + } + + case <-ctx.Done(): } }() diff --git a/sweepbatcher/sweep_batcher.go b/sweepbatcher/sweep_batcher.go index c8ee03b7..f87abcaf 100644 --- a/sweepbatcher/sweep_batcher.go +++ b/sweepbatcher/sweep_batcher.go @@ -1154,74 +1154,70 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep, infof("Batcher monitoring spend for swap %x", sweep.swapHash[:6]) - for { + select { + case spend := <-spendChan: + spendTx := spend.SpendingTx + // Calculate the fee portion that each sweep should pay + // for the batch. + feePortionPerSweep, roundingDifference := + getFeePortionForSweep( + spendTx, len(spendTx.TxIn), + totalSwept, + ) + + onChainFeePortion := getFeePortionPaidBySweep( + spendTx, feePortionPerSweep, + roundingDifference, sweep, + ) + + // Notify the requester of the spend with the spend + // details, including the fee portion for this + // particular sweep. + spendDetail := &SpendDetail{ + Tx: spendTx, + OnChainFeePortion: onChainFeePortion, + } + select { - case spend := <-spendChan: - spendTx := spend.SpendingTx - // Calculate the fee portion that each sweep - // should pay for the batch. - feePortionPerSweep, roundingDifference := - getFeePortionForSweep( - spendTx, len(spendTx.TxIn), - totalSwept, - ) - - onChainFeePortion := getFeePortionPaidBySweep( - spendTx, feePortionPerSweep, - roundingDifference, sweep, - ) - - // Notify the requester of the spend - // with the spend details, including the fee - // portion for this particular sweep. - spendDetail := &SpendDetail{ - Tx: spendTx, - OnChainFeePortion: onChainFeePortion, - } - - select { - // Try to write the update to the notification - // channel. - case notifier.SpendChan <- spendDetail: - - // If a quit signal was provided by the swap, - // continue. - case <-notifier.QuitChan: - - // If the context was canceled, stop. - case <-ctx.Done(): - } - - return - - case err := <-spendErr: - select { - // Try to write the error to the notification - // channel. - case notifier.SpendErrChan <- err: - - // If a quit signal was provided by the swap, - // continue. - case <-notifier.QuitChan: - - // If the context was canceled, stop. - case <-ctx.Done(): - } - - b.writeToErrChan( - ctx, fmt.Errorf("spend error: %w", err), - ) - - return + // Try to write the update to the notification channel. + case notifier.SpendChan <- spendDetail: // If a quit signal was provided by the swap, continue. case <-notifier.QuitChan: - return // If the context was canceled, stop. case <-ctx.Done(): - return } + + return + + case err := <-spendErr: + select { + // Try to write the error to the notification + // channel. + case notifier.SpendErrChan <- err: + + // If a quit signal was provided by the swap, + // continue. + case <-notifier.QuitChan: + + // If the context was canceled, stop. + case <-ctx.Done(): + } + + b.writeToErrChan( + ctx, fmt.Errorf("spend error: %w", err), + ) + + return + + // If a quit signal was provided by the swap, continue. + case <-notifier.QuitChan: + return + + // If the context was canceled, stop. + case <-ctx.Done(): + return } }()