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

Method saveBlock

p2pserver/block_sync.go:713–753  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

711}
712
713func (this *BlockSyncMgr) saveBlock() {
714 if this.tryGetSaveBlockLock() {
715 return
716 }
717 defer this.releaseSaveBlockLock()
718 curBlockHeight := this.ledger.GetCurrentBlockHeight()
719 nextBlockHeight := curBlockHeight + 1
720 this.clearBlocks(curBlockHeight)
721 for {
722 fromID, nextBlock, merkleRoot := this.getBlockCache(nextBlockHeight)
723 if nextBlock == nil {
724 return
725 }
726 err := this.ledger.AddBlock(nextBlock, merkleRoot)
727 this.delBlockCache(nextBlockHeight)
728 if err != nil {
729 this.addErrorRespCnt(fromID)
730 n := this.getNodeWeight(fromID)
731 if n != nil && n.GetErrorRespCnt() >= SYNC_MAX_ERROR_RESP_TIMES {
732 this.delNode(fromID)
733 }
734 log.Warnf("[p2p]saveBlock Height:%d AddBlock error:%s", nextBlockHeight, err)
735 reqNode := this.getNextNode(nextBlockHeight)
736 if reqNode == nil {
737 return
738 }
739 this.addFlightBlock(reqNode.GetID(), nextBlockHeight, nextBlock.Hash())
740 msg := msgpack.NewBlkDataReq(nextBlock.Hash())
741 err := this.server.Send(reqNode, msg, false)
742 if err != nil {
743 log.Warn("[p2p]require new block error:", err)
744 return
745 } else {
746 this.appendReqTime(reqNode.GetID())
747 }
748 return
749 }
750 nextBlockHeight++
751 this.pingOutsyncNodes(nextBlockHeight - 1)
752 }
753}
754
755func (this *BlockSyncMgr) isInBlockCache(blockHeight uint32) bool {
756 this.lock.RLock()

Callers 3

StartMethod · 0.95
OnHeaderReceiveMethod · 0.95
OnBlockReceiveMethod · 0.95

Calls 15

tryGetSaveBlockLockMethod · 0.95
releaseSaveBlockLockMethod · 0.95
clearBlocksMethod · 0.95
getBlockCacheMethod · 0.95
delBlockCacheMethod · 0.95
addErrorRespCntMethod · 0.95
getNodeWeightMethod · 0.95
delNodeMethod · 0.95
getNextNodeMethod · 0.95
addFlightBlockMethod · 0.95
appendReqTimeMethod · 0.95
pingOutsyncNodesMethod · 0.95

Tested by

no test coverage detected