4 "github.com/sirupsen/logrus"
5 "gopkg.in/karalabe/cookiejar.v2/collections/prque"
7 "github.com/vapor/p2p/security"
8 "github.com/vapor/protocol/bc"
17 // blockFetcher is responsible for accumulating block announcements from various peers
18 // and scheduling them for retrieval.
19 type blockFetcher struct {
23 newBlockCh chan *blockMsg
25 msgSet map[bc.Hash]*blockMsg
28 //NewBlockFetcher creates a block fetcher to retrieve blocks of the new propose.
29 func newBlockFetcher(chain Chain, peers Peers) *blockFetcher {
33 newBlockCh: make(chan *blockMsg, newBlockChSize),
35 msgSet: make(map[bc.Hash]*blockMsg),
41 func (f *blockFetcher) blockProcessor() {
43 for !f.queue.Empty() {
44 msg := f.queue.PopItem().(*blockMsg)
45 if msg.block.Height > f.chain.BestBlockHeight()+1 {
46 f.queue.Push(msg, -float32(msg.block.Height))
51 delete(f.msgSet, msg.block.Hash())
57 func (f *blockFetcher) add(msg *blockMsg) {
58 bestHeight := f.chain.BestBlockHeight()
59 if len(f.msgSet) > maxMsgSetSize || bestHeight > msg.block.Height || msg.block.Height-bestHeight > maxBlockDistance {
63 blockHash := msg.block.Hash()
64 if _, ok := f.msgSet[blockHash]; !ok {
65 f.msgSet[blockHash] = msg
66 f.queue.Push(msg, -float32(msg.block.Height))
67 logrus.WithFields(logrus.Fields{
69 "block height": msg.block.Height,
70 "block hash": blockHash.String(),
71 }).Debug("blockFetcher receive propose block")
75 func (f *blockFetcher) insert(msg *blockMsg) {
76 isOrphan, err := f.chain.ProcessBlock(msg.block)
78 peer := f.peers.GetPeer(msg.peerID)
83 f.peers.ProcessIllegal(msg.peerID, security.LevelMsgIllegal, err.Error())
91 proposeMsg, err := NewBlockProposeMsg(msg.block)
93 logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed on create BlockProposeMsg")
97 if err := f.peers.BroadcastMsg(NewBroadcastMsg(proposeMsg, consensusChannel)); err != nil {
98 logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed on broadcast proposed block")
103 func (f *blockFetcher) processNewBlock(msg *blockMsg) {