Skip to content

Commit 6efd010

Browse files
committed
sweepbatcher: run batch with currentHeight set
Prevent a crash with "a height hint greater than 0 must be provided" error when monitorSpend starts at the beginning of batch.Run. The timer timerChan is now initialized at the start, because it was previously initialized after the first block (the current tip) was read from blockChan and now the first block is read before the main for-select loop to fill the field currentHeight in advance.
1 parent 89fbf9a commit 6efd010

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,16 @@ func (b *batch) Run(ctx context.Context) error {
620620
return err
621621
}
622622

623+
// Set currentHeight here, because it may be needed in monitorSpend.
624+
select {
625+
case b.currentHeight = <-blockChan:
626+
b.log.Debugf("initial height for the batch is %v",
627+
b.currentHeight)
628+
629+
case <-runCtx.Done():
630+
return runCtx.Err()
631+
}
632+
623633
// If a primary sweep exists we immediately start monitoring for its
624634
// spend.
625635
if b.primarySweepID != lntypes.ZeroHash {
@@ -636,9 +646,9 @@ func (b *batch) Run(ctx context.Context) error {
636646
skipBefore := clock.Now().Add(b.cfg.initialDelay)
637647

638648
// initialDelayChan is a timer which fires upon initial delay end.
639-
// If initialDelay is 0, it does not fire to prevent race with
640-
// blockChan which also fires immediately with current tip. Such a race
641-
// may result in double publishing if batchPublishDelay is also 0.
649+
// If initialDelay is set to 0, it will not trigger to avoid setting up
650+
// timerChan twice, which could lead to double publishing if
651+
// batchPublishDelay is also 0.
642652
var initialDelayChan <-chan time.Time
643653
if b.cfg.initialDelay > 0 {
644654
initialDelayChan = clock.TickAfter(b.cfg.initialDelay)
@@ -647,9 +657,10 @@ func (b *batch) Run(ctx context.Context) error {
647657
// We use a timer in order to not publish new transactions at the same
648658
// time as the block epoch notification. This is done to prevent
649659
// unnecessary transaction publishments when a spend is detected on that
650-
// block. This timer starts after new block arrives or initialDelay
660+
// block. This timer starts after new block arrives (including the
661+
// current tip which we read from blockChan above) or when initialDelay
651662
// completes.
652-
var timerChan <-chan time.Time
663+
timerChan := clock.TickAfter(b.cfg.batchPublishDelay)
653664

654665
b.log.Infof("started, primary %x, total sweeps %v",
655666
b.primarySweepID[0:6], len(b.sweeps))

0 commit comments

Comments
 (0)