From 299217c6299263e4444911b47fd4a2a3a6ed9bbb Mon Sep 17 00:00:00 2001 From: frozen <355847+Frozen@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:24:46 -0400 Subject: [PATCH] Fix for estimate height --- api/service/stagedstreamsync/downloader.go | 18 ++++++++++ api/service/stagedstreamsync/syncing.go | 39 +++++++++++----------- api/service/stagedstreamsync/types.go | 7 ++++ 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/api/service/stagedstreamsync/downloader.go b/api/service/stagedstreamsync/downloader.go index 9d564b016c..88d6e98da6 100644 --- a/api/service/stagedstreamsync/downloader.go +++ b/api/service/stagedstreamsync/downloader.go @@ -214,6 +214,24 @@ func (d *Downloader) loop() { } go trigger() + go func() { + for { + select { + case <-d.closeC: + return + case <-time.After(60 * time.Second): + h, err := d.stagedSyncInstance.estimateCurrentNumber(context.Background()) + if err != nil { + utils.Logger().Err(err).Msg("failed to estimate current number") + return + } + //TODO: use directly currentCycle var + d.stagedSyncInstance.status.setTargetBN(h) + + } + } + }() + for { select { case <-ticker.C: diff --git a/api/service/stagedstreamsync/syncing.go b/api/service/stagedstreamsync/syncing.go index b996b516a8..de1a23ef80 100644 --- a/api/service/stagedstreamsync/syncing.go +++ b/api/service/stagedstreamsync/syncing.go @@ -308,20 +308,26 @@ func (s *StagedStreamSync) doSync(downloaderContext context.Context, initSync bo return 0, 0, err } - var estimatedHeight uint64 - if initSync { - if h, err := s.estimateCurrentNumber(downloaderContext); err != nil { - return 0, 0, err - } else { - estimatedHeight = h - //TODO: use directly currentCycle var - s.status.setTargetBN(estimatedHeight) - } - if curBN := s.CurrentBlockNumber(); estimatedHeight <= curBN { - s.logger.Info().Uint64("current number", curBN).Uint64("target number", estimatedHeight). - Msg(WrapStagedSyncMsg("early return of long range sync (chain is already ahead of target height)")) - return estimatedHeight, 0, nil - } + var estimatedHeight uint64 = s.status.getTargetBN() + //if initSync { + // if h, err := s.estimateCurrentNumber(downloaderContext); err != nil { + // return 0, 0, err + // } else { + // estimatedHeight = h + // //TODO: use directly currentCycle var + // s.status.setTargetBN(estimatedHeight) + // } + // if curBN := s.CurrentBlockNumber(); estimatedHeight <= curBN { + // s.logger.Info().Uint64("current number", curBN).Uint64("target number", estimatedHeight). + // Msg(WrapStagedSyncMsg("early return of long range sync (chain is already ahead of target height)")) + // return estimatedHeight, 0, nil + // } + //} + + if curBN := s.CurrentBlockNumber(); estimatedHeight <= curBN { + s.logger.Info().Uint64("current number", curBN).Uint64("target number", estimatedHeight). + Msg(WrapStagedSyncMsg("early return of long range sync (chain is already ahead of target height)")) + return estimatedHeight, 0, nil } // We are probably in full sync, but we might have rewound to before the @@ -531,11 +537,6 @@ func (s *StagedStreamSync) estimateCurrentNumber(ctx context.Context) (uint64, e wg.Wait() if len(cnResults) == 0 { - select { - case <-ctx.Done(): - return 0, ctx.Err() - default: - } return 0, ErrZeroBlockResponse } bn := computeBlockNumberByMaxVote(cnResults) diff --git a/api/service/stagedstreamsync/types.go b/api/service/stagedstreamsync/types.go index e46b614299..59c80be2da 100644 --- a/api/service/stagedstreamsync/types.go +++ b/api/service/stagedstreamsync/types.go @@ -40,6 +40,13 @@ func (s *status) setTargetBN(val uint64) { s.targetBN = val } +func (s *status) getTargetBN() uint64 { + s.lock.Lock() + defer s.lock.Unlock() + + return s.targetBN +} + func (s *status) finishSyncing() { s.lock.Lock() defer s.lock.Unlock()