OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / netsync / block_fetcher.go
1 package netsync
2
3 import (
4         log "github.com/sirupsen/logrus"
5         "gopkg.in/karalabe/cookiejar.v2/collections/prque"
6
7         "github.com/vapor/chain"
8         "github.com/vapor/protocol/bc"
9 )
10
11 const (
12         maxBlockDistance = 64
13         maxMsgSetSize    = 128
14         newBlockChSize   = 64
15 )
16
17 // blockFetcher is responsible for accumulating block announcements from various peers
18 // and scheduling them for retrieval.
19 type blockFetcher struct {
20         chain chain.Chain
21         peers *peerSet
22
23         newBlockCh chan *blockMsg
24         queue      *prque.Prque
25         msgSet     map[bc.Hash]*blockMsg
26 }
27
28 //NewBlockFetcher creates a block fetcher to retrieve blocks of the new mined.
29 func newBlockFetcher(chain chain.Chain, peers *peerSet) *blockFetcher {
30         f := &blockFetcher{
31                 chain:      chain,
32                 peers:      peers,
33                 newBlockCh: make(chan *blockMsg, newBlockChSize),
34                 queue:      prque.New(),
35                 msgSet:     make(map[bc.Hash]*blockMsg),
36         }
37         go f.blockProcessor()
38         return f
39 }
40
41 func (f *blockFetcher) blockProcessor() {
42         for {
43                 height := f.chain.BestBlockHeight()
44                 for !f.queue.Empty() {
45                         msg := f.queue.PopItem().(*blockMsg)
46                         if msg.block.Height > height+1 {
47                                 f.queue.Push(msg, -float32(msg.block.Height))
48                                 break
49                         }
50
51                         f.insert(msg)
52                         delete(f.msgSet, msg.block.Hash())
53                 }
54                 f.add(<-f.newBlockCh)
55         }
56 }
57
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 {
61                 return
62         }
63
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                 log.WithFields(log.Fields{
69                         "module":       logModule,
70                         "block height": msg.block.Height,
71                         "block hash":   blockHash.String(),
72                 }).Debug("blockFetcher receive mine block")
73         }
74 }
75
76 func (f *blockFetcher) insert(msg *blockMsg) {
77         if _, err := f.chain.ProcessBlock(msg.block); err != nil {
78                 peer := f.peers.getPeer(msg.peerID)
79                 if peer == nil {
80                         return
81                 }
82
83                 f.peers.addBanScore(msg.peerID, 20, 0, err.Error())
84                 return
85         }
86
87         if err := f.peers.broadcastMinedBlock(msg.block); err != nil {
88                 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("blockFetcher fail on broadcast new block")
89                 return
90         }
91 }
92
93 func (f *blockFetcher) processNewBlock(msg *blockMsg) {
94         f.newBlockCh <- msg
95 }