@@ -418,7 +418,24 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
418418 // can't attach its notifier to the batch as that is no longer running.
419419 // Instead we directly detect and return the spend here.
420420 if completed && * notifier != (SpendNotifier {}) {
421- return b .monitorSpendAndNotify (ctx , sweep , notifier )
421+ // Verify that the parent batch is confirmed. Note that a batch
422+ // is only considered confirmed after it has received three
423+ // on-chain confirmations to prevent issues caused by reorgs.
424+ parentBatch , err := b .store .GetParentBatch (ctx , sweep .swapHash )
425+ if err != nil {
426+ log .Errorf ("unable to get parent batch for sweep %x: " +
427+ "%v" , sweep .swapHash [:6 ], err )
428+
429+ return err
430+ }
431+
432+ // The parent batch is indeed confirmed, meaning it is complete
433+ // and we won't be able to attach this sweep to it.
434+ if parentBatch .State == batchConfirmed {
435+ return b .monitorSpendAndNotify (
436+ ctx , sweep , parentBatch .ID , notifier ,
437+ )
438+ }
422439 }
423440
424441 sweep .notifier = notifier
@@ -688,19 +705,13 @@ func (b *Batcher) FetchUnconfirmedBatches(ctx context.Context) ([]*batch,
688705// monitorSpendAndNotify monitors the spend of a specific outpoint and writes
689706// the response back to the response channel.
690707func (b * Batcher ) monitorSpendAndNotify (ctx context.Context , sweep * sweep ,
691- notifier * SpendNotifier ) error {
708+ parentBatchID int32 , notifier * SpendNotifier ) error {
692709
693710 spendCtx , cancel := context .WithCancel (ctx )
694711 defer cancel ()
695712
696- // First get the batch that completed the sweep.
697- parentBatch , err := b .store .GetParentBatch (ctx , sweep .swapHash )
698- if err != nil {
699- return err
700- }
701-
702713 // Then we get the total amount that was swept by the batch.
703- totalSwept , err := b .store .TotalSweptAmount (ctx , parentBatch . ID )
714+ totalSwept , err := b .store .TotalSweptAmount (ctx , parentBatchID )
704715 if err != nil {
705716 return err
706717 }
0 commit comments