From e1ceb9db49a88b01b12a0414a19a336832731c4a Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Thu, 9 Jan 2025 01:43:30 -0300 Subject: [PATCH] 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. --- sweepbatcher/sweep_batch.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/sweepbatcher/sweep_batch.go b/sweepbatcher/sweep_batch.go index 302136c96..e3a1e5959 100644 --- a/sweepbatcher/sweep_batch.go +++ b/sweepbatcher/sweep_batch.go @@ -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 { @@ -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) @@ -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))