@@ -222,6 +222,18 @@ type SweepRequest struct {
222
222
Notifier * SpendNotifier
223
223
}
224
224
225
+ // addSweepsRequest is a request to sweep an outpoint or a group of outpoints
226
+ // that is used internally by the batcher (between AddSweep and handleSweeps).
227
+ type addSweepsRequest struct {
228
+ // sweeps is the list of sweeps already loaded from DB and fee rate
229
+ // source.
230
+ sweeps []* sweep
231
+
232
+ // Notifier is a notifier that is used to notify the requester of this
233
+ // sweep that the sweep was successful.
234
+ notifier * SpendNotifier
235
+ }
236
+
225
237
type SpendDetail struct {
226
238
// Tx is the transaction that spent the outpoint.
227
239
Tx * wire.MsgTx
@@ -266,8 +278,8 @@ type Batcher struct {
266
278
// batches is a map of batch IDs to the currently active batches.
267
279
batches map [int32 ]* batch
268
280
269
- // sweepReqs is a channel where sweep requests are received.
270
- sweepReqs chan SweepRequest
281
+ // addSweepsChan is a channel where sweep requests are received.
282
+ addSweepsChan chan * addSweepsRequest
271
283
272
284
// testReqs is a channel where test requests are received.
273
285
// This is used only in unit tests! The reason to have this is to
@@ -501,7 +513,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
501
513
502
514
return & Batcher {
503
515
batches : make (map [int32 ]* batch ),
504
- sweepReqs : make (chan SweepRequest ),
516
+ addSweepsChan : make (chan * addSweepsRequest ),
505
517
testReqs : make (chan * testRequest ),
506
518
errChan : make (chan error , 1 ),
507
519
quit : make (chan struct {}),
@@ -557,15 +569,8 @@ func (b *Batcher) Run(ctx context.Context) error {
557
569
558
570
for {
559
571
select {
560
- case sweepReq := <- b .sweepReqs :
561
- sweeps , err := b .fetchSweeps (runCtx , sweepReq )
562
- if err != nil {
563
- warnf ("fetchSweeps failed: %v." , err )
564
-
565
- return err
566
- }
567
-
568
- err = b .handleSweeps (runCtx , sweeps , sweepReq .Notifier )
572
+ case req := <- b .addSweepsChan :
573
+ err = b .handleSweeps (runCtx , req .sweeps , req .notifier )
569
574
if err != nil {
570
575
warnf ("handleSweeps failed: %v." , err )
571
576
@@ -589,11 +594,32 @@ func (b *Batcher) Run(ctx context.Context) error {
589
594
}
590
595
}
591
596
592
- // AddSweep adds a sweep request to the batcher for handling. This will either
593
- // place the sweep in an existing batch or create a new one.
594
- func (b * Batcher ) AddSweep (sweepReq * SweepRequest ) error {
597
+ // AddSweep loads information about sweeps from the store and fee rate source,
598
+ // and adds them to the batcher for handling. This will either place the sweep
599
+ // in an existing batch or create a new one. The method can be called multiple
600
+ // times, but the sweeps (including the order of them) must be the same. If
601
+ // notifier is provided, the batcher sends back sweeping results through it.
602
+ func (b * Batcher ) AddSweep (ctx context.Context , sweepReq * SweepRequest ) error {
603
+ // If the batcher is shutting down, quit now.
604
+ select {
605
+ case <- b .quit :
606
+ return ErrBatcherShuttingDown
607
+
608
+ default :
609
+ }
610
+
611
+ sweeps , err := b .fetchSweeps (ctx , * sweepReq )
612
+ if err != nil {
613
+ return fmt .Errorf ("fetchSweeps failed: %w" , err )
614
+ }
615
+
616
+ req := & addSweepsRequest {
617
+ sweeps : sweeps ,
618
+ notifier : sweepReq .Notifier ,
619
+ }
620
+
595
621
select {
596
- case b .sweepReqs <- * sweepReq :
622
+ case b .addSweepsChan <- req :
597
623
return nil
598
624
599
625
case <- b .quit :
0 commit comments