()
| 711 | } |
| 712 | |
| 713 | func (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 | |
| 755 | func (this *BlockSyncMgr) isInBlockCache(blockHeight uint32) bool { |
| 756 | this.lock.RLock() |
no test coverage detected