()
| 343 | } |
| 344 | |
| 345 | func (this *BlockSyncMgr) checkTimeout() { |
| 346 | now := time.Now() |
| 347 | headerTimeoutFlights := make(map[uint32]*SyncFlightInfo, 0) |
| 348 | blockTimeoutFlights := make(map[common.Uint256][]*SyncFlightInfo, 0) |
| 349 | this.lock.RLock() |
| 350 | for height, flightInfo := range this.flightHeaders { |
| 351 | if int(now.Sub(flightInfo.startTime).Seconds()) >= SYNC_HEADER_REQUEST_TIMEOUT { |
| 352 | headerTimeoutFlights[height] = flightInfo |
| 353 | } |
| 354 | } |
| 355 | for blockHash, flightInfos := range this.flightBlocks { |
| 356 | for _, flightInfo := range flightInfos { |
| 357 | if int(now.Sub(flightInfo.startTime).Seconds()) >= SYNC_BLOCK_REQUEST_TIMEOUT { |
| 358 | blockTimeoutFlights[blockHash] = append(blockTimeoutFlights[blockHash], flightInfo) |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | this.lock.RUnlock() |
| 363 | |
| 364 | curHeaderHeight := this.ledger.GetCurrentHeaderHeight() |
| 365 | curBlockHeight := this.ledger.GetCurrentBlockHeight() |
| 366 | |
| 367 | for height, flightInfo := range headerTimeoutFlights { |
| 368 | this.addTimeoutCnt(flightInfo.GetNodeId()) |
| 369 | if height <= curHeaderHeight { |
| 370 | this.delFlightHeader(height) |
| 371 | continue |
| 372 | } |
| 373 | flightInfo.ResetStartTime() |
| 374 | flightInfo.MarkFailedNode() |
| 375 | log.Tracef("[p2p]checkTimeout sync headers from id:%d :%d timeout after:%d s Times:%d", flightInfo.GetNodeId(), height, SYNC_HEADER_REQUEST_TIMEOUT, flightInfo.GetTotalFailedTimes()) |
| 376 | reqNode := this.getNodeWithMinFailedTimes(flightInfo, curBlockHeight) |
| 377 | if reqNode == nil { |
| 378 | break |
| 379 | } |
| 380 | flightInfo.SetNodeId(reqNode.GetID()) |
| 381 | |
| 382 | headerHash := this.ledger.GetCurrentHeaderHash() |
| 383 | msg := msgpack.NewHeadersReq(headerHash) |
| 384 | err := this.server.Send(reqNode, msg, false) |
| 385 | if err != nil { |
| 386 | log.Warn("[p2p]checkTimeout failed to send a new headersReq:s", err) |
| 387 | } else { |
| 388 | this.appendReqTime(reqNode.GetID()) |
| 389 | } |
| 390 | } |
| 391 | for blockHash, flightInfos := range blockTimeoutFlights { |
| 392 | for _, flightInfo := range flightInfos { |
| 393 | this.addTimeoutCnt(flightInfo.GetNodeId()) |
| 394 | if flightInfo.Height <= curBlockHeight { |
| 395 | this.delFlightBlock(blockHash) |
| 396 | continue |
| 397 | } |
| 398 | flightInfo.ResetStartTime() |
| 399 | flightInfo.MarkFailedNode() |
| 400 | log.Tracef("[p2p]checkTimeout sync height:%d block:0x%x timeout after:%d s times:%d", flightInfo.Height, blockHash, SYNC_BLOCK_REQUEST_TIMEOUT, flightInfo.GetTotalFailedTimes()) |
| 401 | reqNode := this.getNodeWithMinFailedTimes(flightInfo, curBlockHeight) |
| 402 | if reqNode == nil { |
no test coverage detected