Skip to content

Commit

Permalink
sweepbatcher: run batch with currentHeight set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
starius committed Jan 9, 2025
1 parent b22a914 commit e1ceb9d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions sweepbatcher/sweep_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,16 @@ func (b *batch) Run(ctx context.Context) error {
return err
}

// Set currentHeight here, because it may be needed in monitorSpend.
select {
case b.currentHeight = <-blockChan:
b.log.Debugf("initial height for the batch is %v",
b.currentHeight)

case <-runCtx.Done():
return runCtx.Err()
}

// If a primary sweep exists we immediately start monitoring for its
// spend.
if b.primarySweepID != lntypes.ZeroHash {
Expand All @@ -636,9 +646,8 @@ func (b *batch) Run(ctx context.Context) error {
skipBefore := clock.Now().Add(b.cfg.initialDelay)

// initialDelayChan is a timer which fires upon initial delay end.
// If initialDelay is 0, it does not fire to prevent race with
// blockChan which also fires immediately with current tip. Such a race
// may result in double publishing if batchPublishDelay is also 0.
// If initialDelay is 0, it does not fire not to install timerChan twice
// which may result in double publishing if batchPublishDelay is also 0.
var initialDelayChan <-chan time.Time
if b.cfg.initialDelay > 0 {
initialDelayChan = clock.TickAfter(b.cfg.initialDelay)
Expand All @@ -647,9 +656,10 @@ func (b *batch) Run(ctx context.Context) error {
// We use a timer in order to not publish new transactions at the same
// time as the block epoch notification. This is done to prevent
// unnecessary transaction publishments when a spend is detected on that
// block. This timer starts after new block arrives or initialDelay
// block. This timer starts after new block arrives (including the
// current tip which we read from blockChan above) or when initialDelay
// completes.
var timerChan <-chan time.Time
timerChan := clock.TickAfter(b.cfg.batchPublishDelay)

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

0 comments on commit e1ceb9d

Please sign in to comment.