diff --git a/api/service/syncing/downloader/client.go b/api/service/syncing/downloader/client.go index 1c4573e9c..d17c5e24c 100644 --- a/api/service/syncing/downloader/client.go +++ b/api/service/syncing/downloader/client.go @@ -110,7 +110,7 @@ func (client *Client) PushNewBlock(selfPeerHash [20]byte, blockHash []byte, time // GetBlockChainHeight gets the blockheight from peer func (client *Client) GetBlockChainHeight() *pb.DownloaderResponse { - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHEIGHT} response, err := client.dlClient.Query(ctx, request) diff --git a/api/service/syncing/syncing.go b/api/service/syncing/syncing.go index ad10ffb02..3c542362c 100644 --- a/api/service/syncing/syncing.go +++ b/api/service/syncing/syncing.go @@ -74,7 +74,6 @@ type StateSync struct { selfPeerHash [20]byte // hash of ip and address combination peerNumber int activePeerNumber int - currentHeight uint64 // current height of local blockchain commonBlocks map[int]*types.Block lastMileBlocks []*types.Block // last mile blocks to catch up with the consensus syncConfig *SyncConfig @@ -449,7 +448,6 @@ func (ss *StateSync) updateBlockAndStatus(block *types.Block, bc *core.BlockChai return false } ss.syncMux.Lock() - ss.currentHeight = bc.CurrentBlock().NumberU64() worker.UpdateCurrent() ss.syncMux.Unlock() utils.GetLogInstance().Info("[SYNC] new block added to blockchain", "blockHeight", bc.CurrentBlock().NumberU64(), "blockHex", bc.CurrentBlock().Hash().Hex()) @@ -584,15 +582,17 @@ func (ss *StateSync) getMaxPeerHeight() uint64 { } // IsOutOfSync checks whether the node is out of sync from other peers -func (ss *StateSync) IsOutOfSync() bool { +func (ss *StateSync) IsOutOfSync(bc *core.BlockChain) bool { otherHeight := ss.getMaxPeerHeight() - return ss.currentHeight+inSyncThreshold < otherHeight + currentHeight := bc.CurrentBlock().NumberU64() + utils.GetLogInstance().Debug("[SYNC] IsOutOfSync", "otherHeight", otherHeight, "myHeight", currentHeight) + return currentHeight+inSyncThreshold < otherHeight } // SyncLoop will keep syncing with peers until catches up func (ss *StateSync) SyncLoop(bc *core.BlockChain, worker *worker.Worker, willJoinConsensus bool) { for { - if !ss.IsOutOfSync() { + if !ss.IsOutOfSync(bc) { utils.GetLogInstance().Info("[SYNC] Node is now IN SYNC!") return } diff --git a/node/node_syncing.go b/node/node_syncing.go index 4af51f323..5bcf694d7 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -78,7 +78,7 @@ SyncingLoop: continue SyncingLoop } } - if node.stateSync.IsOutOfSync() { + if node.stateSync.IsOutOfSync(bc) { utils.GetLogInstance().Debug("[SYNC] out of sync, doing syncing") node.stateMutex.Lock() node.State = NodeNotInSync