@@ -222,6 +222,18 @@ type SweepRequest struct {
222222 Notifier * SpendNotifier
223223}
224224
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+
225237type SpendDetail struct {
226238 // Tx is the transaction that spent the outpoint.
227239 Tx * wire.MsgTx
@@ -266,8 +278,8 @@ type Batcher struct {
266278 // batches is a map of batch IDs to the currently active batches.
267279 batches map [int32 ]* batch
268280
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
271283
272284 // testReqs is a channel where test requests are received.
273285 // This is used only in unit tests! The reason to have this is to
@@ -501,7 +513,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
501513
502514 return & Batcher {
503515 batches : make (map [int32 ]* batch ),
504- sweepReqs : make (chan SweepRequest ),
516+ addSweepsChan : make (chan * addSweepsRequest ),
505517 testReqs : make (chan * testRequest ),
506518 errChan : make (chan error , 1 ),
507519 quit : make (chan struct {}),
@@ -557,15 +569,8 @@ func (b *Batcher) Run(ctx context.Context) error {
557569
558570 for {
559571 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 )
569574 if err != nil {
570575 warnf ("handleSweeps failed: %v." , err )
571576
@@ -589,11 +594,32 @@ func (b *Batcher) Run(ctx context.Context) error {
589594 }
590595}
591596
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+
595621 select {
596- case b .sweepReqs <- * sweepReq :
622+ case b .addSweepsChan <- req :
597623 return nil
598624
599625 case <- b .quit :
0 commit comments