Skip to content

Commit fa1bc48

Browse files
committed
sweepbatcher: remove unneeded for loops
The loop always had exactly one iteration.
1 parent 01664ad commit fa1bc48

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

1825-
for {
1825+
select {
1826+
case spend := <-spendChan:
18261827
select {
1827-
case spend := <-spendChan:
1828-
select {
1829-
case b.spendChan <- spend:
1830-
1831-
case <-ctx.Done():
1832-
}
1828+
case b.spendChan <- spend:
18331829

1834-
return
1830+
case <-ctx.Done():
1831+
}
18351832

1836-
case err := <-spendErr:
1837-
b.writeToSpendErrChan(ctx, err)
1833+
case err := <-spendErr:
1834+
b.writeToSpendErrChan(ctx, err)
18381835

1839-
b.writeToErrChan(
1840-
fmt.Errorf("spend error: %w", err),
1841-
)
1842-
1843-
return
1836+
b.writeToErrChan(
1837+
fmt.Errorf("spend error: %w", err),
1838+
)
18441839

1845-
case <-ctx.Done():
1846-
return
1847-
}
1840+
case <-ctx.Done():
18481841
}
18491842
}()
18501843

@@ -1878,39 +1871,31 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
18781871
defer cancel()
18791872
defer b.wg.Done()
18801873

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

1882+
case err := <-errChan:
1883+
b.writeToErrChan(fmt.Errorf("confirmations "+
1884+
"monitoring error: %w", err))
1885+
1886+
case <-reorgChan:
1887+
// A re-org has been detected. We set the batch
1888+
// state back to open since our batch
1889+
// transaction is no longer present in any
1890+
// block. We can accept more sweeps and try to
1891+
// publish new transactions, at this point we
1892+
// need to monitor again for a new spend.
1893+
select {
1894+
case b.reorgChan <- struct{}{}:
19111895
case <-ctx.Done():
1912-
return
19131896
}
1897+
1898+
case <-ctx.Done():
19141899
}
19151900
}()
19161901

sweepbatcher/sweep_batcher.go

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

1157-
for {
1158-
select {
1159-
case spend := <-spendChan:
1160-
spendTx := spend.SpendingTx
1161-
// Calculate the fee portion that each sweep
1162-
// should pay for the batch.
1163-
feePortionPerSweep, roundingDifference :=
1164-
getFeePortionForSweep(
1165-
spendTx, len(spendTx.TxIn),
1166-
totalSwept,
1167-
)
1168-
1169-
onChainFeePortion := getFeePortionPaidBySweep(
1170-
spendTx, feePortionPerSweep,
1171-
roundingDifference, sweep,
1157+
select {
1158+
case spend := <-spendChan:
1159+
spendTx := spend.SpendingTx
1160+
// Calculate the fee portion that each sweep should pay
1161+
// for the batch.
1162+
feePortionPerSweep, roundingDifference :=
1163+
getFeePortionForSweep(
1164+
spendTx, len(spendTx.TxIn),
1165+
totalSwept,
11721166
)
11731167

1174-
// Notify the requester of the spend
1175-
// with the spend details, including the fee
1176-
// portion for this particular sweep.
1177-
spendDetail := &SpendDetail{
1178-
Tx: spendTx,
1179-
OnChainFeePortion: onChainFeePortion,
1180-
}
1181-
1182-
select {
1183-
// Try to write the update to the notification
1184-
// channel.
1185-
case notifier.SpendChan <- spendDetail:
1186-
1187-
// If a quit signal was provided by the swap,
1188-
// continue.
1189-
case <-notifier.QuitChan:
1190-
1191-
// If the context was canceled, stop.
1192-
case <-ctx.Done():
1193-
}
1194-
1195-
return
1196-
1197-
case err := <-spendErr:
1198-
select {
1199-
// Try to write the error to the notification
1200-
// channel.
1201-
case notifier.SpendErrChan <- err:
1202-
1203-
// If a quit signal was provided by the swap,
1204-
// continue.
1205-
case <-notifier.QuitChan:
1206-
1207-
// If the context was canceled, stop.
1208-
case <-ctx.Done():
1209-
}
1210-
1211-
b.writeToErrChan(
1212-
ctx, fmt.Errorf("spend error: %w", err),
1213-
)
1168+
onChainFeePortion := getFeePortionPaidBySweep(
1169+
spendTx, feePortionPerSweep,
1170+
roundingDifference, sweep,
1171+
)
12141172

1215-
return
1173+
// Notify the requester of the spend with the spend
1174+
// details, including the fee portion for this
1175+
// particular sweep.
1176+
spendDetail := &SpendDetail{
1177+
Tx: spendTx,
1178+
OnChainFeePortion: onChainFeePortion,
1179+
}
1180+
1181+
select {
1182+
// Try to write the update to the notification channel.
1183+
case notifier.SpendChan <- spendDetail:
12161184

12171185
// If a quit signal was provided by the swap, continue.
12181186
case <-notifier.QuitChan:
1219-
return
12201187

12211188
// If the context was canceled, stop.
12221189
case <-ctx.Done():
1223-
return
12241190
}
1191+
1192+
return
1193+
1194+
case err := <-spendErr:
1195+
select {
1196+
// Try to write the error to the notification
1197+
// channel.
1198+
case notifier.SpendErrChan <- err:
1199+
1200+
// If a quit signal was provided by the swap,
1201+
// continue.
1202+
case <-notifier.QuitChan:
1203+
1204+
// If the context was canceled, stop.
1205+
case <-ctx.Done():
1206+
}
1207+
1208+
b.writeToErrChan(
1209+
ctx, fmt.Errorf("spend error: %w", err),
1210+
)
1211+
1212+
return
1213+
1214+
// If a quit signal was provided by the swap, continue.
1215+
case <-notifier.QuitChan:
1216+
return
1217+
1218+
// If the context was canceled, stop.
1219+
case <-ctx.Done():
1220+
return
12251221
}
12261222
}()
12271223

0 commit comments

Comments
 (0)