Skip to content

Commit

Permalink
go/consensus/tendermint: sync-worker additionally check block timestamps
Browse files Browse the repository at this point in the history
Sync-worker relies on Tendermint fast-sync to determine if the node is still
catching up. This PR adds aditional condition that the latest block is not
older than 1 minute. This prevents cases where node stops fast-syincing
before it has actually caught up.
  • Loading branch information
ptrus committed Apr 28, 2020
1 parent cf7736d commit 918f240
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changelog/2873.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go/consensus/tendermint: sync-worker additionally check block timestamps

Sync-worker relied on Tendermint fast-sync to determine if the node is still
catching up. This PR adds aditional condition that the latest block is not
older than 1 minute. This prevents cases where node stops fast-syincing
before it has actually caught up.
34 changes: 30 additions & 4 deletions go/consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ const (
// StateDir is the name of the directory located inside the node's data
// directory which contains the tendermint state.
StateDir = "tendermint"

// Time difference threshold used when considering if node is done with
// initial syncing. If difference is greater than the specified threshold
// the node is considered not yet synced.
// NOTE: this is only used during the initial sync.
syncWorkerLastBlockTimeDiffThreshold = 1 * time.Minute
)

var (
Expand Down Expand Up @@ -1173,17 +1179,37 @@ func (t *tendermintService) syncWorker() {
case <-t.node.Quit():
return
case <-time.After(1 * time.Second):
isSyncing, err := checkSyncFn()
isFastSyncing, err := checkSyncFn()
if err != nil {
t.Logger.Error("Failed to poll FastSync",
"err", err,
)
return
}
if !isSyncing {
if !isFastSyncing {
t.Logger.Info("Tendermint Node finished fast-sync")
close(t.syncedCh)
return

// Check latest block time.
tmBlock, err := t.GetTendermintBlock(t.ctx, consensusAPI.HeightLatest)
if err != nil {
t.Logger.Error("Failed to get tendermint block",
"err", err,
)
return
}

now := time.Now()
// No committed blocks or latest block within threshold.
if tmBlock == nil || now.Sub(tmBlock.Header.Time) < syncWorkerLastBlockTimeDiffThreshold {
close(t.syncedCh)
return
}

t.Logger.Debug("Node still syncing",
"currentTime", now,
"latestBlockTime", tmBlock.Time,
"diff", now.Sub(tmBlock.Time),
)
}
}
}
Expand Down

0 comments on commit 918f240

Please sign in to comment.