Skip to content

Commit a616fbd

Browse files
committed
sweepbatcher: do not fail on restoring empty batches
Previously storing an empty batch would make the batcher fail to start as spinning up a restored batch assumes that there's a primary sweep added already. As there's no point in spinning up such batch we can just skip over it. Furthermore we'll ensure that we won't try to ever publish an empty batch to avoid setting the fee rate too early.
1 parent 38f0e3a commit a616fbd

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/hex"
7+
"errors"
78
"fmt"
89
"math"
910
"sync"
@@ -48,7 +49,7 @@ const (
4849
)
4950

5051
var (
51-
ErrBatchShuttingDown = fmt.Errorf("batch shutting down")
52+
ErrBatchShuttingDown = errors.New("batch shutting down")
5253
)
5354

5455
// sweep stores any data related to sweeping a specific outpoint.
@@ -539,6 +540,12 @@ func (b *batch) publish(ctx context.Context) error {
539540
coopSuccess bool
540541
)
541542

543+
if len(b.sweeps) == 0 {
544+
b.log.Debugf("skipping publish: no sweeps in the batch")
545+
546+
return nil
547+
}
548+
542549
// Run the RBF rate update.
543550
err = b.updateRbfRate(ctx)
544551
if err != nil {

sweepbatcher/sweep_batcher.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sweepbatcher
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"sync"
78
"time"
@@ -135,7 +136,7 @@ type SpendNotifier struct {
135136
}
136137

137138
var (
138-
ErrBatcherShuttingDown = fmt.Errorf("batcher shutting down")
139+
ErrBatcherShuttingDown = errors.New("batcher shutting down")
139140
)
140141

141142
// Batcher is a system that is responsible for accepting sweep requests and
@@ -306,7 +307,7 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
306307

307308
if batch.sweepExists(sweep.swapHash) {
308309
accepted, err := batch.addSweep(ctx, sweep)
309-
if err != nil {
310+
if err != nil && !errors.Is(err, ErrBatchShuttingDown) {
310311
return err
311312
}
312313

@@ -321,7 +322,7 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
321322
// If one of the batches accepts the sweep, we provide it to that batch.
322323
for _, batch := range b.batches {
323324
accepted, err := batch.addSweep(ctx, sweep)
324-
if err != nil && err != ErrBatchShuttingDown {
325+
if err != nil {
325326
return err
326327
}
327328

@@ -407,23 +408,16 @@ func (b *Batcher) spinUpBatch(ctx context.Context) (*batch, error) {
407408
// spinUpBatchDB spins up a batch that already existed in storage, then
408409
// returns it.
409410
func (b *Batcher) spinUpBatchFromDB(ctx context.Context, batch *batch) error {
410-
cfg := batchConfig{
411-
maxTimeoutDistance: batch.cfg.maxTimeoutDistance,
412-
batchConfTarget: defaultBatchConfTarget,
413-
}
414-
415-
rbfCache := rbfCache{
416-
LastHeight: batch.rbfCache.LastHeight,
417-
FeeRate: batch.rbfCache.FeeRate,
418-
}
419-
420411
dbSweeps, err := b.store.FetchBatchSweeps(ctx, batch.id)
421412
if err != nil {
422413
return err
423414
}
424415

425416
if len(dbSweeps) == 0 {
426-
return fmt.Errorf("batch %d has no sweeps", batch.id)
417+
log.Infof("skipping restored batch %d as it has no sweeps",
418+
batch.id)
419+
420+
return nil
427421
}
428422

429423
primarySweep := dbSweeps[0]
@@ -439,6 +433,11 @@ func (b *Batcher) spinUpBatchFromDB(ctx context.Context, batch *batch) error {
439433
sweeps[sweep.swapHash] = *sweep
440434
}
441435

436+
rbfCache := rbfCache{
437+
LastHeight: batch.rbfCache.LastHeight,
438+
FeeRate: batch.rbfCache.FeeRate,
439+
}
440+
442441
batchKit := batchKit{
443442
id: batch.id,
444443
batchTxid: batch.batchTxid,
@@ -458,6 +457,11 @@ func (b *Batcher) spinUpBatchFromDB(ctx context.Context, batch *batch) error {
458457
log: batchPrefixLogger(fmt.Sprintf("%d", batch.id)),
459458
}
460459

460+
cfg := batchConfig{
461+
maxTimeoutDistance: batch.cfg.maxTimeoutDistance,
462+
batchConfTarget: defaultBatchConfTarget,
463+
}
464+
461465
newBatch := NewBatchFromDB(cfg, batchKit)
462466

463467
// We add the batch to our map of batches and start it.

0 commit comments

Comments
 (0)