fix initial blockheight checking when restart the node

pull/647/head
Ubuntu 6 years ago committed by chaosma
parent d2d08cdd50
commit 43d39bc5bd
  1. 2
      api/service/syncing/downloader/client.go
  2. 10
      api/service/syncing/syncing.go
  3. 2
      node/node_syncing.go

@ -110,7 +110,7 @@ func (client *Client) PushNewBlock(selfPeerHash [20]byte, blockHash []byte, time
// GetBlockChainHeight gets the blockheight from peer // GetBlockChainHeight gets the blockheight from peer
func (client *Client) GetBlockChainHeight() *pb.DownloaderResponse { 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() defer cancel()
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHEIGHT} request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHEIGHT}
response, err := client.dlClient.Query(ctx, request) response, err := client.dlClient.Query(ctx, request)

@ -74,7 +74,6 @@ type StateSync struct {
selfPeerHash [20]byte // hash of ip and address combination selfPeerHash [20]byte // hash of ip and address combination
peerNumber int peerNumber int
activePeerNumber int activePeerNumber int
currentHeight uint64 // current height of local blockchain
commonBlocks map[int]*types.Block commonBlocks map[int]*types.Block
lastMileBlocks []*types.Block // last mile blocks to catch up with the consensus lastMileBlocks []*types.Block // last mile blocks to catch up with the consensus
syncConfig *SyncConfig syncConfig *SyncConfig
@ -449,7 +448,6 @@ func (ss *StateSync) updateBlockAndStatus(block *types.Block, bc *core.BlockChai
return false return false
} }
ss.syncMux.Lock() ss.syncMux.Lock()
ss.currentHeight = bc.CurrentBlock().NumberU64()
worker.UpdateCurrent() worker.UpdateCurrent()
ss.syncMux.Unlock() ss.syncMux.Unlock()
utils.GetLogInstance().Info("[SYNC] new block added to blockchain", "blockHeight", bc.CurrentBlock().NumberU64(), "blockHex", bc.CurrentBlock().Hash().Hex()) 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 // 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() 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 // SyncLoop will keep syncing with peers until catches up
func (ss *StateSync) SyncLoop(bc *core.BlockChain, worker *worker.Worker, willJoinConsensus bool) { func (ss *StateSync) SyncLoop(bc *core.BlockChain, worker *worker.Worker, willJoinConsensus bool) {
for { for {
if !ss.IsOutOfSync() { if !ss.IsOutOfSync(bc) {
utils.GetLogInstance().Info("[SYNC] Node is now IN SYNC!") utils.GetLogInstance().Info("[SYNC] Node is now IN SYNC!")
return return
} }

@ -78,7 +78,7 @@ SyncingLoop:
continue SyncingLoop continue SyncingLoop
} }
} }
if node.stateSync.IsOutOfSync() { if node.stateSync.IsOutOfSync(bc) {
utils.GetLogInstance().Debug("[SYNC] out of sync, doing syncing") utils.GetLogInstance().Debug("[SYNC] out of sync, doing syncing")
node.stateMutex.Lock() node.stateMutex.Lock()
node.State = NodeNotInSync node.State = NodeNotInSync

Loading…
Cancel
Save