Skip to content

Commit fbfc4f5

Browse files
committed
sweepbatcher: remove unneded for loops
The loop always had exactly one iteration.
1 parent 7001cec commit fbfc4f5

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
@@ -1155,74 +1155,70 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
11551155
infof("Batcher monitoring spend for swap %x",
11561156
sweep.swapHash[:6])
11571157

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

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

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

12181186
// If a quit signal was provided by the swap, continue.
12191187
case <-notifier.QuitChan:
1220-
return
12211188

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

0 commit comments

Comments
 (0)