Skip to content

Commit 4b44967

Browse files
committed
sweepbatcher: remove unneded for loops
The loop always had exactly one iteration.
1 parent 97dee06 commit 4b44967

File tree

2 files changed

+86
-105
lines changed

2 files changed

+86
-105
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,29 +1678,22 @@ func (b *batch) monitorSpend(ctx context.Context, primarySweep sweep) error {
16781678
b.Infof("monitoring spend for outpoint %s",
16791679
primarySweep.outpoint.String())
16801680

1681-
for {
1681+
select {
1682+
case spend := <-spendChan:
16821683
select {
1683-
case spend := <-spendChan:
1684-
select {
1685-
case b.spendChan <- spend:
1686-
1687-
case <-ctx.Done():
1688-
}
1684+
case b.spendChan <- spend:
16891685

1690-
return
1691-
1692-
case err := <-spendErr:
1693-
b.writeToSpendErrChan(ctx, err)
1686+
case <-ctx.Done():
1687+
}
16941688

1695-
b.writeToErrChan(
1696-
fmt.Errorf("spend error: %w", err),
1697-
)
1689+
case err := <-spendErr:
1690+
b.writeToSpendErrChan(ctx, err)
16981691

1699-
return
1692+
b.writeToErrChan(
1693+
fmt.Errorf("spend error: %w", err),
1694+
)
17001695

1701-
case <-ctx.Done():
1702-
return
1703-
}
1696+
case <-ctx.Done():
17041697
}
17051698
}()
17061699

@@ -1734,39 +1727,31 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
17341727
defer cancel()
17351728
defer b.wg.Done()
17361729

1737-
for {
1730+
select {
1731+
case conf := <-confChan:
17381732
select {
1739-
case conf := <-confChan:
1740-
select {
1741-
case b.confChan <- conf:
1742-
1743-
case <-ctx.Done():
1744-
}
1745-
1746-
return
1747-
1748-
case err := <-errChan:
1749-
b.writeToErrChan(fmt.Errorf("confirmations "+
1750-
"monitoring error: %w", err))
1751-
1752-
return
1753-
1754-
case <-reorgChan:
1755-
// A re-org has been detected. We set the batch
1756-
// state back to open since our batch
1757-
// transaction is no longer present in any
1758-
// block. We can accept more sweeps and try to
1759-
// publish new transactions, at this point we
1760-
// need to monitor again for a new spend.
1761-
select {
1762-
case b.reorgChan <- struct{}{}:
1763-
case <-ctx.Done():
1764-
}
1765-
return
1733+
case b.confChan <- conf:
1734+
1735+
case <-ctx.Done():
1736+
}
17661737

1738+
case err := <-errChan:
1739+
b.writeToErrChan(fmt.Errorf("confirmations "+
1740+
"monitoring error: %w", err))
1741+
1742+
case <-reorgChan:
1743+
// A re-org has been detected. We set the batch
1744+
// state back to open since our batch
1745+
// transaction is no longer present in any
1746+
// block. We can accept more sweeps and try to
1747+
// publish new transactions, at this point we
1748+
// need to monitor again for a new spend.
1749+
select {
1750+
case b.reorgChan <- struct{}{}:
17671751
case <-ctx.Done():
1768-
return
17691752
}
1753+
1754+
case <-ctx.Done():
17701755
}
17711756
}()
17721757

sweepbatcher/sweep_batcher.go

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -977,74 +977,70 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
977977
infof("Batcher monitoring spend for swap %x",
978978
sweep.swapHash[:6])
979979

980-
for {
981-
select {
982-
case spend := <-spendChan:
983-
spendTx := spend.SpendingTx
984-
// Calculate the fee portion that each sweep
985-
// should pay for the batch.
986-
feePortionPerSweep, roundingDifference :=
987-
getFeePortionForSweep(
988-
spendTx, len(spendTx.TxIn),
989-
totalSwept,
990-
)
991-
992-
onChainFeePortion := getFeePortionPaidBySweep(
993-
spendTx, feePortionPerSweep,
994-
roundingDifference, sweep,
980+
select {
981+
case spend := <-spendChan:
982+
spendTx := spend.SpendingTx
983+
// Calculate the fee portion that each sweep should pay
984+
// for the batch.
985+
feePortionPerSweep, roundingDifference :=
986+
getFeePortionForSweep(
987+
spendTx, len(spendTx.TxIn),
988+
totalSwept,
995989
)
996990

997-
// Notify the requester of the spend
998-
// with the spend details, including the fee
999-
// portion for this particular sweep.
1000-
spendDetail := &SpendDetail{
1001-
Tx: spendTx,
1002-
OnChainFeePortion: onChainFeePortion,
1003-
}
1004-
1005-
select {
1006-
// Try to write the update to the notification
1007-
// channel.
1008-
case notifier.SpendChan <- spendDetail:
1009-
1010-
// If a quit signal was provided by the swap,
1011-
// continue.
1012-
case <-notifier.QuitChan:
1013-
1014-
// If the context was canceled, stop.
1015-
case <-ctx.Done():
1016-
}
1017-
1018-
return
1019-
1020-
case err := <-spendErr:
1021-
select {
1022-
// Try to write the error to the notification
1023-
// channel.
1024-
case notifier.SpendErrChan <- err:
1025-
1026-
// If a quit signal was provided by the swap,
1027-
// continue.
1028-
case <-notifier.QuitChan:
1029-
1030-
// If the context was canceled, stop.
1031-
case <-ctx.Done():
1032-
}
1033-
1034-
b.writeToErrChan(
1035-
ctx, fmt.Errorf("spend error: %w", err),
1036-
)
991+
onChainFeePortion := getFeePortionPaidBySweep(
992+
spendTx, feePortionPerSweep,
993+
roundingDifference, sweep,
994+
)
1037995

1038-
return
996+
// Notify the requester of the spend with the spend
997+
// details, including the fee portion for this
998+
// particular sweep.
999+
spendDetail := &SpendDetail{
1000+
Tx: spendTx,
1001+
OnChainFeePortion: onChainFeePortion,
1002+
}
1003+
1004+
select {
1005+
// Try to write the update to the notification channel.
1006+
case notifier.SpendChan <- spendDetail:
10391007

10401008
// If a quit signal was provided by the swap, continue.
10411009
case <-notifier.QuitChan:
1042-
return
10431010

10441011
// If the context was canceled, stop.
10451012
case <-ctx.Done():
1046-
return
10471013
}
1014+
1015+
return
1016+
1017+
case err := <-spendErr:
1018+
select {
1019+
// Try to write the error to the notification
1020+
// channel.
1021+
case notifier.SpendErrChan <- err:
1022+
1023+
// If a quit signal was provided by the swap,
1024+
// continue.
1025+
case <-notifier.QuitChan:
1026+
1027+
// If the context was canceled, stop.
1028+
case <-ctx.Done():
1029+
}
1030+
1031+
b.writeToErrChan(
1032+
ctx, fmt.Errorf("spend error: %w", err),
1033+
)
1034+
1035+
return
1036+
1037+
// If a quit signal was provided by the swap, continue.
1038+
case <-notifier.QuitChan:
1039+
return
1040+
1041+
// If the context was canceled, stop.
1042+
case <-ctx.Done():
1043+
return
10481044
}
10491045
}()
10501046

0 commit comments

Comments
 (0)