Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for estimate height #4779

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/service/stagedstreamsync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 20 additions & 19 deletions api/service/stagedstreamsync/syncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions api/service/stagedstreamsync/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down