Skip to content

Commit

Permalink
Minor fixes for isSyncing check
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyilong committed Mar 1, 2021
1 parent b588c44 commit 783f0c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ledger/state/storeview.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (sv *StoreView) IncrementHeight() {
func (sv *StoreView) Save() common.Hash {
rootHash, err := sv.store.Commit()

logger.Infof("Commit to data store, height: %v, rootHash: %v", sv.height+1, rootHash.Hex())
logger.Debugf("Commit to data store, height: %v, rootHash: %v", sv.height+1, rootHash.Hex())

if err != nil {
log.Panicf("Failed to save the StoreView: %v", err)
Expand Down
29 changes: 20 additions & 9 deletions rpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (t *ThetaRPCService) GetBlockByHeight(args *GetBlockByHeightArgs, result *G

type GetBlocksByRangeArgs struct {
Start common.JSONUint64 `json:"start"`
End common.JSONUint64 `json:"end"`
End common.JSONUint64 `json:"end"`
}

func (t *ThetaRPCService) GetBlocksByRange(args *GetBlocksByRangeArgs, result *GetBlocksResult) (err error) {
Expand All @@ -376,7 +376,7 @@ func (t *ThetaRPCService) GetBlocksByRange(args *GetBlocksByRangeArgs, result *G
return errors.New("Starting block must be less than ending block")
}

if args.End - args.Start > 100 {
if args.End-args.Start > 100 {
return errors.New("Can't retrieve more than 100 blocks at a time")
}

Expand Down Expand Up @@ -464,16 +464,16 @@ func (t *ThetaRPCService) GetStatus(args *GetStatusArgs, result *GetStatusResult
result.PeerID = t.dispatcher.LibP2PID() // TODO: use ID() instead after 1.3.0 upgrade
result.ChainID = t.consensus.Chain().ChainID
latestFinalizedHash := s.LastFinalizedBlock
var latestFinalizedBlock *core.ExtendedBlock
if !latestFinalizedHash.IsEmpty() {
result.LatestFinalizedBlockHash = latestFinalizedHash
block, err := t.chain.FindBlock(latestFinalizedHash)
latestFinalizedBlock, err = t.chain.FindBlock(latestFinalizedHash)
if err != nil {
return err
}
result.LatestFinalizedBlockEpoch = common.JSONUint64(block.Epoch)
result.LatestFinalizedBlockHeight = common.JSONUint64(block.Height)
result.LatestFinalizedBlockTime = (*common.JSONBig)(block.Timestamp)
result.Syncing = isSyncing(block)
result.LatestFinalizedBlockEpoch = common.JSONUint64(latestFinalizedBlock.Epoch)
result.LatestFinalizedBlockHeight = common.JSONUint64(latestFinalizedBlock.Height)
result.LatestFinalizedBlockTime = (*common.JSONBig)(latestFinalizedBlock.Timestamp)
}
result.CurrentEpoch = common.JSONUint64(s.Epoch)
result.CurrentTime = (*common.JSONBig)(big.NewInt(time.Now().Unix()))
Expand All @@ -491,6 +491,9 @@ func (t *ThetaRPCService) GetStatus(args *GetStatusArgs, result *GetStatusResult
}
result.CurrentHeight = common.JSONUint64(maxVoteHeight - 1) // current finalized height is at most maxVoteHeight-1
}

result.Syncing = isSyncing(latestFinalizedBlock, uint64(result.CurrentHeight))

return
}

Expand Down Expand Up @@ -666,10 +669,18 @@ func getTxType(tx types.Tx) byte {
return t
}

func isSyncing(block *core.ExtendedBlock) bool {
func isSyncing(lastestFinalizedBlock *core.ExtendedBlock, currentHeight uint64) bool {
if lastestFinalizedBlock == nil {
return true
}
currentTime := big.NewInt(time.Now().Unix())
maxDiff := new(big.Int).SetUint64(30) // thirty seconds, about 5 blocks
threshold := new(big.Int).Sub(currentTime, maxDiff)
isSyncing := block.Timestamp.Cmp(threshold) < 0
isSyncing := lastestFinalizedBlock.Timestamp.Cmp(threshold) < 0

if isSyncing { // sometimes the validator node clock is off, so here we also compare the block heights
isSyncing = (currentHeight - lastestFinalizedBlock.Height) > 5
}

return isSyncing
}

0 comments on commit 783f0c5

Please sign in to comment.