OnHeaderReceive receive header from net
(fromID uint64, headers []*types.Header)
| 532 | |
| 533 | //OnHeaderReceive receive header from net |
| 534 | func (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 |
| 590 | func (this *BlockSyncMgr) OnBlockReceive(fromID uint64, blockSize uint32, block *types.Block, |
nothing calls this directly
no test coverage detected