MCPcopy Create free account
hub / github.com/DNAProject/DNA / syncBlock

Method syncBlock

p2pserver/block_sync.go:462–531  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

460}
461
462func (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)

Callers 2

syncMethod · 0.95
OnBlockReceiveMethod · 0.95

Calls 15

tryGetSyncBlockLockMethod · 0.95
releaseSyncBlockLockMethod · 0.95
getFlightBlockCountMethod · 0.95
getNonEmptyBlockCountMethod · 0.95
isBlockOnFlightMethod · 0.95
isInBlockCacheMethod · 0.95
getNextNodeMethod · 0.95
addFlightBlockMethod · 0.95
appendReqTimeMethod · 0.95
WarnfFunction · 0.92
GetCurrentBlockHeightMethod · 0.65

Tested by

no test coverage detected