4 "github.com/sirupsen/logrus"
5 "gopkg.in/karalabe/cookiejar.v2/collections/prque"
7 "github.com/vapor/netsync/peers"
8 "github.com/vapor/p2p/security"
9 "github.com/vapor/protocol/bc"
18 // blockFetcher is responsible for accumulating block announcements from various peers
19 // and scheduling them for retrieval.
20 type blockFetcher struct {
24 newBlockCh chan *blockMsg
26 msgSet map[bc.Hash]*blockMsg
29 //NewBlockFetcher creates a block fetcher to retrieve blocks of the new propose.
30 func newBlockFetcher(chain Chain, peers *peers.PeerSet) *blockFetcher {
34 newBlockCh: make(chan *blockMsg, newBlockChSize),
36 msgSet: make(map[bc.Hash]*blockMsg),
42 func (f *blockFetcher) blockProcessor() {
44 for !f.queue.Empty() {
45 msg := f.queue.PopItem().(*blockMsg)
46 if msg.block.Height > f.chain.BestBlockHeight()+1 {
47 f.queue.Push(msg, -float32(msg.block.Height))
52 delete(f.msgSet, msg.block.Hash())
58 func (f *blockFetcher) add(msg *blockMsg) {
59 bestHeight := f.chain.BestBlockHeight()
60 if len(f.msgSet) > maxMsgSetSize || bestHeight > msg.block.Height || msg.block.Height-bestHeight > maxBlockDistance {
64 blockHash := msg.block.Hash()
65 if _, ok := f.msgSet[blockHash]; !ok {
66 f.msgSet[blockHash] = msg
67 f.queue.Push(msg, -float32(msg.block.Height))
68 logrus.WithFields(logrus.Fields{
70 "block height": msg.block.Height,
71 "block hash": blockHash.String(),
72 }).Debug("blockFetcher receive propose block")
76 func (f *blockFetcher) insert(msg *blockMsg) {
77 isOrphan, err := f.chain.ProcessBlock(msg.block)
79 peer := f.peers.GetPeer(msg.peerID)
84 f.peers.ProcessIllegal(msg.peerID, security.LevelMsgIllegal, err.Error())
92 proposeMsg, err := NewBlockProposeMsg(msg.block)
94 logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed on create BlockProposeMsg")
98 if err := f.peers.BroadcastMsg(NewBroadcastMsg(proposeMsg, consensusChannel)); err != nil {
99 logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed on broadcast proposed block")
104 func (f *blockFetcher) processNewBlock(msg *blockMsg) {