@@ -418,7 +418,24 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
418
418
// can't attach its notifier to the batch as that is no longer running.
419
419
// Instead we directly detect and return the spend here.
420
420
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
+ }
422
439
}
423
440
424
441
sweep .notifier = notifier
@@ -688,19 +705,13 @@ func (b *Batcher) FetchUnconfirmedBatches(ctx context.Context) ([]*batch,
688
705
// monitorSpendAndNotify monitors the spend of a specific outpoint and writes
689
706
// the response back to the response channel.
690
707
func (b * Batcher ) monitorSpendAndNotify (ctx context.Context , sweep * sweep ,
691
- notifier * SpendNotifier ) error {
708
+ parentBatchID int32 , notifier * SpendNotifier ) error {
692
709
693
710
spendCtx , cancel := context .WithCancel (ctx )
694
711
defer cancel ()
695
712
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
-
702
713
// 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 )
704
715
if err != nil {
705
716
return err
706
717
}
0 commit comments