()
| 460 | } |
| 461 | |
| 462 | func (this *BlockSyncMgr) syncBlock() { |
| 463 | if this.tryGetSyncBlockLock() { |
| 464 | return |
| 465 | } |
| 466 | defer this.releaseSyncBlockLock() |
| 467 | |
| 468 | availCount := SYNC_MAX_FLIGHT_BLOCK_SIZE - this.getFlightBlockCount() |
| 469 | if availCount <= 0 { |
| 470 | return |
| 471 | } |
| 472 | curBlockHeight := this.ledger.GetCurrentBlockHeight() |
| 473 | curHeaderHeight := this.ledger.GetCurrentHeaderHeight() |
| 474 | count := int(curHeaderHeight - curBlockHeight) |
| 475 | if count <= 0 { |
| 476 | return |
| 477 | } |
| 478 | if count > availCount { |
| 479 | count = availCount |
| 480 | } |
| 481 | cacheCap := SYNC_MAX_BLOCK_CACHE_SIZE - this.getNonEmptyBlockCount() |
| 482 | if count > cacheCap { |
| 483 | count = cacheCap |
| 484 | } |
| 485 | |
| 486 | counter := 1 |
| 487 | i := uint32(0) |
| 488 | reqTimes := 1 |
| 489 | for { |
| 490 | if counter > count { |
| 491 | break |
| 492 | } |
| 493 | i++ |
| 494 | nextBlockHeight := curBlockHeight + i |
| 495 | nextBlockHash := this.ledger.GetBlockHash(nextBlockHeight) |
| 496 | if nextBlockHash == common.UINT256_EMPTY { |
| 497 | return |
| 498 | } |
| 499 | if this.isBlockOnFlight(nextBlockHash) { |
| 500 | if nextBlockHeight <= curBlockHeight+SYNC_NEXT_BLOCKS_HEIGHT { |
| 501 | //request more nodes for next block height |
| 502 | reqTimes = SYNC_NEXT_BLOCK_TIMES |
| 503 | } else { |
| 504 | continue |
| 505 | } |
| 506 | } |
| 507 | if this.isInBlockCache(nextBlockHeight) { |
| 508 | continue |
| 509 | } |
| 510 | if nextBlockHeight <= curBlockHeight+SYNC_NEXT_BLOCKS_HEIGHT { |
| 511 | reqTimes = SYNC_NEXT_BLOCK_TIMES |
| 512 | } |
| 513 | for t := 0; t < reqTimes; t++ { |
| 514 | reqNode := this.getNextNode(nextBlockHeight) |
| 515 | if reqNode == nil { |
| 516 | return |
| 517 | } |
| 518 | this.addFlightBlock(reqNode.GetID(), nextBlockHeight, nextBlockHash) |
| 519 | msg := msgpack.NewBlkDataReq(nextBlockHash) |
no test coverage detected