Skip to content

Commit 1848e70

Browse files
committed
sweepbatcher: remove unneded for loops
The loop always had exactly one iteration.
1 parent a851a17 commit 1848e70

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
@@ -1816,29 +1816,22 @@ func (b *batch) monitorSpend(ctx context.Context, primarySweep sweep) error {
18161816
b.Infof("monitoring spend for outpoint %s",
18171817
primarySweep.outpoint.String())
18181818

1819-
for {
1819+
select {
1820+
case spend := <-spendChan:
18201821
select {
1821-
case spend := <-spendChan:
1822-
select {
1823-
case b.spendChan <- spend:
1824-
1825-
case <-ctx.Done():
1826-
}
1822+
case b.spendChan <- spend:
18271823

1828-
return
1824+
case <-ctx.Done():
1825+
}
18291826

1830-
case err := <-spendErr:
1831-
b.writeToSpendErrChan(ctx, err)
1827+
case err := <-spendErr:
1828+
b.writeToSpendErrChan(ctx, err)
18321829

1833-
b.writeToErrChan(
1834-
fmt.Errorf("spend error: %w", err),
1835-
)
1836-
1837-
return
1830+
b.writeToErrChan(
1831+
fmt.Errorf("spend error: %w", err),
1832+
)
18381833

1839-
case <-ctx.Done():
1840-
return
1841-
}
1834+
case <-ctx.Done():
18421835
}
18431836
}()
18441837

@@ -1872,39 +1865,31 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
18721865
defer cancel()
18731866
defer b.wg.Done()
18741867

1875-
for {
1868+
select {
1869+
case conf := <-confChan:
18761870
select {
1877-
case conf := <-confChan:
1878-
select {
1879-
case b.confChan <- conf:
1880-
1881-
case <-ctx.Done():
1882-
}
1883-
1884-
return
1885-
1886-
case err := <-errChan:
1887-
b.writeToErrChan(fmt.Errorf("confirmations "+
1888-
"monitoring error: %w", err))
1889-
1890-
return
1891-
1892-
case <-reorgChan:
1893-
// A re-org has been detected. We set the batch
1894-
// state back to open since our batch
1895-
// transaction is no longer present in any
1896-
// block. We can accept more sweeps and try to
1897-
// publish new transactions, at this point we
1898-
// need to monitor again for a new spend.
1899-
select {
1900-
case b.reorgChan <- struct{}{}:
1901-
case <-ctx.Done():
1902-
}
1903-
return
1871+
case b.confChan <- conf:
1872+
1873+
case <-ctx.Done():
1874+
}
19041875

1876+
case err := <-errChan:
1877+
b.writeToErrChan(fmt.Errorf("confirmations "+
1878+
"monitoring error: %w", err))
1879+
1880+
case <-reorgChan:
1881+
// A re-org has been detected. We set the batch
1882+
// state back to open since our batch
1883+
// transaction is no longer present in any
1884+
// block. We can accept more sweeps and try to
1885+
// publish new transactions, at this point we
1886+
// need to monitor again for a new spend.
1887+
select {
1888+
case b.reorgChan <- struct{}{}:
19051889
case <-ctx.Done():
1906-
return
19071890
}
1891+
1892+
case <-ctx.Done():
19081893
}
19091894
}()
19101895

sweepbatcher/sweep_batcher.go

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

1092-
for {
1093-
select {
1094-
case spend := <-spendChan:
1095-
spendTx := spend.SpendingTx
1096-
// Calculate the fee portion that each sweep
1097-
// should pay for the batch.
1098-
feePortionPerSweep, roundingDifference :=
1099-
getFeePortionForSweep(
1100-
spendTx, len(spendTx.TxIn),
1101-
totalSwept,
1102-
)
1103-
1104-
onChainFeePortion := getFeePortionPaidBySweep(
1105-
spendTx, feePortionPerSweep,
1106-
roundingDifference, sweep,
1092+
select {
1093+
case spend := <-spendChan:
1094+
spendTx := spend.SpendingTx
1095+
// Calculate the fee portion that each sweep should pay
1096+
// for the batch.
1097+
feePortionPerSweep, roundingDifference :=
1098+
getFeePortionForSweep(
1099+
spendTx, len(spendTx.TxIn),
1100+
totalSwept,
11071101
)
11081102

1109-
// Notify the requester of the spend
1110-
// with the spend details, including the fee
1111-
// portion for this particular sweep.
1112-
spendDetail := &SpendDetail{
1113-
Tx: spendTx,
1114-
OnChainFeePortion: onChainFeePortion,
1115-
}
1116-
1117-
select {
1118-
// Try to write the update to the notification
1119-
// channel.
1120-
case notifier.SpendChan <- spendDetail:
1121-
1122-
// If a quit signal was provided by the swap,
1123-
// continue.
1124-
case <-notifier.QuitChan:
1125-
1126-
// If the context was canceled, stop.
1127-
case <-ctx.Done():
1128-
}
1129-
1130-
return
1131-
1132-
case err := <-spendErr:
1133-
select {
1134-
// Try to write the error to the notification
1135-
// channel.
1136-
case notifier.SpendErrChan <- err:
1137-
1138-
// If a quit signal was provided by the swap,
1139-
// continue.
1140-
case <-notifier.QuitChan:
1141-
1142-
// If the context was canceled, stop.
1143-
case <-ctx.Done():
1144-
}
1145-
1146-
b.writeToErrChan(
1147-
ctx, fmt.Errorf("spend error: %w", err),
1148-
)
1103+
onChainFeePortion := getFeePortionPaidBySweep(
1104+
spendTx, feePortionPerSweep,
1105+
roundingDifference, sweep,
1106+
)
11491107

1150-
return
1108+
// Notify the requester of the spend with the spend
1109+
// details, including the fee portion for this
1110+
// particular sweep.
1111+
spendDetail := &SpendDetail{
1112+
Tx: spendTx,
1113+
OnChainFeePortion: onChainFeePortion,
1114+
}
1115+
1116+
select {
1117+
// Try to write the update to the notification channel.
1118+
case notifier.SpendChan <- spendDetail:
11511119

11521120
// If a quit signal was provided by the swap, continue.
11531121
case <-notifier.QuitChan:
1154-
return
11551122

11561123
// If the context was canceled, stop.
11571124
case <-ctx.Done():
1158-
return
11591125
}
1126+
1127+
return
1128+
1129+
case err := <-spendErr:
1130+
select {
1131+
// Try to write the error to the notification
1132+
// channel.
1133+
case notifier.SpendErrChan <- err:
1134+
1135+
// If a quit signal was provided by the swap,
1136+
// continue.
1137+
case <-notifier.QuitChan:
1138+
1139+
// If the context was canceled, stop.
1140+
case <-ctx.Done():
1141+
}
1142+
1143+
b.writeToErrChan(
1144+
ctx, fmt.Errorf("spend error: %w", err),
1145+
)
1146+
1147+
return
1148+
1149+
// If a quit signal was provided by the swap, continue.
1150+
case <-notifier.QuitChan:
1151+
return
1152+
1153+
// If the context was canceled, stop.
1154+
case <-ctx.Done():
1155+
return
11601156
}
11611157
}()
11621158

0 commit comments

Comments
 (0)