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

Method OnHeaderReceive

p2pserver/block_sync.go:534–587  ·  view source on GitHub ↗

OnHeaderReceive receive header from net

(fromID uint64, headers []*types.Header)

Source from the content-addressed store, hash-verified

532
533//OnHeaderReceive receive header from net
534func (this *BlockSyncMgr) OnHeaderReceive(fromID uint64, headers []*types.Header) {
535 if len(headers) == 0 {
536 return
537 }
538 log.Infof("Header receive height:%d - %d", headers[0].Height, headers[len(headers)-1].Height)
539 height := headers[0].Height
540 curHeaderHeight := this.ledger.GetCurrentHeaderHeight()
541
542 //Means another gorountinue is adding header
543 if height <= curHeaderHeight {
544 return
545 }
546 if !this.isHeaderOnFlight(height) {
547 return
548 }
549 err := this.ledger.AddHeaders(headers)
550 this.delFlightHeader(height)
551 if err != nil {
552 this.addErrorRespCnt(fromID)
553 n := this.getNodeWeight(fromID)
554 if n != nil && n.GetErrorRespCnt() >= SYNC_MAX_ERROR_RESP_TIMES {
555 this.delNode(fromID)
556 }
557 log.Warnf("[p2p]OnHeaderReceive AddHeaders error:%s", err)
558 return
559 }
560 sort.Slice(headers, func(i, j int) bool {
561 return headers[i].Height < headers[j].Height
562 })
563 curHeaderHeight = this.ledger.GetCurrentHeaderHeight()
564 curBlockHeight := this.ledger.GetCurrentBlockHeight()
565 for _, header := range headers {
566 //handle empty block
567 if header.TransactionsRoot == common.UINT256_EMPTY {
568 log.Trace("[p2p]OnHeaderReceive empty block Height:%d", header.Height)
569 height := header.Height
570 blockHash := header.Hash()
571 this.delFlightBlock(blockHash)
572 nextHeader := curHeaderHeight + 1
573 if height > nextHeader {
574 break
575 }
576 if height <= curBlockHeight {
577 continue
578 }
579 block := &types.Block{
580 Header: header,
581 }
582 this.addBlockCache(fromID, block, common.UINT256_EMPTY)
583 }
584 }
585 go this.saveBlock()
586 this.syncHeader()
587}
588
589// OnBlockReceive receive block from net
590func (this *BlockSyncMgr) OnBlockReceive(fromID uint64, blockSize uint32, block *types.Block,

Callers

nothing calls this directly

Calls 15

isHeaderOnFlightMethod · 0.95
delFlightHeaderMethod · 0.95
addErrorRespCntMethod · 0.95
getNodeWeightMethod · 0.95
delNodeMethod · 0.95
delFlightBlockMethod · 0.95
addBlockCacheMethod · 0.95
saveBlockMethod · 0.95
syncHeaderMethod · 0.95
InfofFunction · 0.92
WarnfFunction · 0.92
TraceFunction · 0.92

Tested by

no test coverage detected