Skip to content

Commit e1ceb9d

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 b22a914 commit e1ceb9d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 15 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,8 @@ 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 0, it does not fire not to install timerChan twice
650+
// which may result in double publishing if batchPublishDelay is also 0.
642651
var initialDelayChan <-chan time.Time
643652
if b.cfg.initialDelay > 0 {
644653
initialDelayChan = clock.TickAfter(b.cfg.initialDelay)
@@ -647,9 +656,10 @@ func (b *batch) Run(ctx context.Context) error {
647656
// We use a timer in order to not publish new transactions at the same
648657
// time as the block epoch notification. This is done to prevent
649658
// unnecessary transaction publishments when a spend is detected on that
650-
// block. This timer starts after new block arrives or initialDelay
659+
// block. This timer starts after new block arrives (including the
660+
// current tip which we read from blockChan above) or when initialDelay
651661
// completes.
652-
var timerChan <-chan time.Time
662+
timerChan := clock.TickAfter(b.cfg.batchPublishDelay)
653663

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

0 commit comments

Comments
 (0)