refactor cleanCache into two for readability; simplify GetBlockHashes request handle logic; requestor will accept only up to batchsize blockhashes instead of reject for longer response

pull/1090/head
chao 5 years ago committed by chaosma
parent 155927992a
commit d46263b46a
  1. 33
      api/service/syncing/syncing.go
  2. 9
      node/node_syncing.go

@ -107,8 +107,19 @@ type StateSync struct {
syncMux sync.Mutex
}
// level = 0 only clean "old" blocks, level > 0 clean up everything
func (ss *StateSync) cleanCache(level int) {
func (ss *StateSync) purgeAllBlocksFromCache() {
ss.syncMux.Lock()
defer ss.syncMux.Unlock()
ss.commonBlocks = make(map[int]*types.Block)
ss.lastMileBlocks = nil
ss.syncConfig.ForEachPeer(func(configPeer *SyncPeerConfig) (brk bool) {
configPeer.blockHashes = nil
configPeer.newBlocks = nil
return
})
}
func (ss *StateSync) purgeOldBlocksFromCache() {
ss.syncMux.Lock()
defer ss.syncMux.Unlock()
ss.commonBlocks = make(map[int]*types.Block)
@ -116,15 +127,6 @@ func (ss *StateSync) cleanCache(level int) {
configPeer.blockHashes = nil
return
})
if level > 0 {
ss.commonBlocks = make(map[int]*types.Block)
ss.lastMileBlocks = nil
ss.syncConfig.ForEachPeer(func(configPeer *SyncPeerConfig) (brk bool) {
configPeer.blockHashes = nil
configPeer.newBlocks = nil
return
})
}
}
// AddLastMileBlock add the lastest a few block into queue for syncing
@ -325,9 +327,10 @@ func (ss *StateSync) GetConsensusHashes(startHash []byte, size uint32) bool {
}
if len(response.Payload) > int(size+1) {
utils.GetLogInstance().Warn("[Sync] GetConsensusHashes: receive more blockHahses than request!", "requestSize", size, "respondSize", len(response.Payload))
return
peerConfig.blockHashes = response.Payload[:size+1]
} else {
peerConfig.blockHashes = response.Payload
}
peerConfig.blockHashes = response.Payload
}()
return
})
@ -680,9 +683,9 @@ func (ss *StateSync) SyncLoop(bc *core.BlockChain, worker *worker.Worker, willJo
size = BatchSize
}
ss.ProcessStateSync(startHash[:], size, bc, worker)
ss.cleanCache(0)
ss.purgeOldBlocksFromCache()
}
ss.cleanCache(1)
ss.purgeAllBlocksFromCache()
}
// GetSyncingPort returns the syncing port.

@ -218,11 +218,12 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest) (*
if startHeight >= endHeight {
return response, fmt.Errorf("[SYNC] GetBlockHashes Request failed. I am not higher than requested node, my Height %v, request node Height %v", endHeight, startHeight)
}
if endHeight-startHeight > size {
endHeight = startHeight + size
}
for blockNum := startHeight; blockNum <= endHeight; blockNum++ {
for blockNum := startHeight; blockNum <= startHeight+size; blockNum++ {
block := node.Blockchain().GetBlockByNumber(blockNum)
if block == nil {
break
}
blockHash := block.Hash()
response.Payload = append(response.Payload, blockHash[:])
}

Loading…
Cancel
Save