Skip to content

Commit

Permalink
feature: Step by step sync (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
calmbeing committed Jun 13, 2023
1 parent 6da3fa5 commit 551d8eb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 5 additions & 7 deletions eth/stagedsync/stage_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func SpawnStageHeaders(
}
defer tx.Rollback()
}
if initialCycle {
// Set sync upperbound only at the initalCycle to avoid repeated set.
cfg.hd.SetStageSyncUpperBound(cfg.StageSyncUpperBound)
cfg.hd.SetStageSyncStep(cfg.StageSyncStep)
}
if initialCycle && cfg.snapshots != nil && cfg.snapshots.Cfg().Enabled {
if err := cfg.hd.AddHeadersFromSnapshot(tx, cfg.snapshots.BlocksAvailable(), cfg.blockReader); err != nil {
return err
Expand Down Expand Up @@ -801,13 +806,6 @@ func HeadersPOW(
headerInserter := headerdownload.NewHeaderInserter(logPrefix, localTd, headerProgress, cfg.blockReader)
cfg.hd.SetHeaderReader(&ChainReaderImpl{config: &cfg.chainConfig, tx: tx, blockReader: cfg.blockReader})

if cfg.StageSyncUpperBound > 0 && cfg.StageSyncStep == 0 {
// if sync upperbound enabled but disabled step sync, then set upperbound, otherwise not to avoid repeated set.
cfg.hd.SetStageSyncUpperBound(cfg.StageSyncUpperBound)
}

cfg.hd.SetStageSyncStep(cfg.StageSyncStep)

stopped := false
var noProgressCounter uint = 0
prevProgress := headerProgress
Expand Down
5 changes: 4 additions & 1 deletion turbo/stages/headerdownload/header_algos.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ func (hd *HeaderDownload) InsertHeader(hf FeedHeaderFunc, terminalTotalDifficult
blocksToTTD = x.Uint64()
}
}
if hd.stageSyncStep > 0 && hd.highestInDb%hd.stageSyncStep == 0 {
return false, true, 0, lastTime, nil
}
return hd.insertQueue.Len() > 0 && hd.insertQueue[0].blockHeight <= hd.highestInDb+1, false, blocksToTTD, lastTime, nil
}

Expand Down Expand Up @@ -1229,7 +1232,7 @@ func (hd *HeaderDownload) SetStageSyncUpperBound(stageSyncUpperBound uint64) {
func (hd *HeaderDownload) SetStageSyncStep(stageSyncStep uint64) {
hd.lock.Lock()
defer hd.lock.Unlock()
hd.stageSyncUpperBound += stageSyncStep
hd.stageSyncStep = stageSyncStep
}

func (hd *HeaderDownload) SetRequestId(requestId int) {
Expand Down
1 change: 1 addition & 0 deletions turbo/stages/headerdownload/header_data_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ type HeaderDownload struct {
posDownloaderTip common.Hash // See https://hackmd.io/GDc0maGsQeKfP8o2C7L52w
badPoSHeaders map[common.Hash]common.Hash // Invalid Tip -> Last Valid Ancestor
stageSyncUpperBound uint64 // Upper bound for stage sync, stop sync upon reached this height
stageSyncStep uint64 // Step for stage sync
}

// HeaderRecord encapsulates two forms of the same header - raw RLP encoding (to avoid duplicated decodings and encodings), and parsed value types.Header
Expand Down

0 comments on commit 551d8eb

Please sign in to comment.