Skip to content

Commit eda10c7

Browse files
committed
downloader: updated downloader and fixed issues with catch up
Properly ignore blocks coming from peers not in our peer list (blocked) and do never request anything from bad peers. Added some checks to account for blocks known when requesting hashes (missing parents).
1 parent dff3955 commit eda10c7

1 file changed

Lines changed: 47 additions & 21 deletions

File tree

‎eth/downloader/downloader.go‎

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ type blockPack struct {
5454
}
5555

5656
type syncPack struct {
57-
peer *peer
58-
hash common.Hash
57+
peer *peer
58+
hash common.Hash
59+
ignoreInitial bool
5960
}
6061

6162
func New(hasBlock hashCheckFn, insertChain chainInsertFn, currentTd currentTdFn) *Downloader {
@@ -104,11 +105,13 @@ func (d *Downloader) UnregisterPeer(id string) {
104105

105106
func (d *Downloader) peerHandler() {
106107
// itimer is used to determine when to start ignoring `minDesiredPeerCount`
107-
itimer := time.NewTicker(5 * time.Second)
108+
//itimer := time.NewTicker(5 * time.Second)
109+
itimer := time.NewTimer(5 * time.Second)
108110
out:
109111
for {
110112
select {
111113
case <-d.newPeerCh:
114+
itimer.Stop()
112115
// Meet the `minDesiredPeerCount` before we select our best peer
113116
if len(d.peers) < minDesiredPeerCount {
114117
break
@@ -137,7 +140,7 @@ func (d *Downloader) selectPeer(p *peer) {
137140
}
138141

139142
glog.V(logger.Detail).Infoln("New peer with highest TD =", p.td)
140-
d.syncCh <- syncPack{p, p.recentHash}
143+
d.syncCh <- syncPack{p, p.recentHash, false}
141144
}
142145
}
143146

@@ -151,7 +154,7 @@ out:
151154
// Start the fetcher. This will block the update entirely
152155
// interupts need to be send to the appropriate channels
153156
// respectively.
154-
if err := d.startFetchingHashes(selectedPeer, sync.hash); err != nil {
157+
if err := d.startFetchingHashes(selectedPeer, sync.hash, sync.ignoreInitial); err != nil {
155158
// handle error
156159
glog.V(logger.Debug).Infoln("Error fetching hashes:", err)
157160
// XXX Reset
@@ -178,11 +181,18 @@ out:
178181
}
179182

180183
// XXX Make synchronous
181-
func (d *Downloader) startFetchingHashes(p *peer, hash common.Hash) error {
182-
glog.V(logger.Debug).Infoln("Downloading hashes")
184+
func (d *Downloader) startFetchingHashes(p *peer, hash common.Hash, ignoreInitial bool) error {
185+
glog.V(logger.Debug).Infof("Downloading hashes (%x) from %s", hash.Bytes()[:4], p.id)
183186

184187
start := time.Now()
185188

189+
// We ignore the initial hash in some cases (e.g. we received a block without it's parent)
190+
// In such circumstances we don't need to download the block so don't add it to the queue.
191+
if !ignoreInitial {
192+
// Add the hash to the queue first
193+
d.queue.hashPool.Add(hash)
194+
}
195+
186196
// Get the first batch of hashes
187197
p.getHashes(hash)
188198
atomic.StoreInt32(&d.fetchingHashes, 1)
@@ -195,7 +205,7 @@ out:
195205
hashSet := set.New()
196206
for _, hash := range hashes {
197207
if d.hasBlock(hash) {
198-
glog.V(logger.Debug).Infof("Found common hash %x\n", hash)
208+
glog.V(logger.Debug).Infof("Found common hash %x\n", hash[:4])
199209

200210
done = true
201211
break
@@ -207,7 +217,7 @@ out:
207217

208218
// Add hashes to the chunk set
209219
// Check if we're done fetching
210-
if !done {
220+
if !done && len(hashes) > 0 {
211221
//fmt.Println("re-fetch. current =", d.queue.hashPool.Size())
212222
// Get the next set of hashes
213223
p.getHashes(hashes[len(hashes)-1])
@@ -218,7 +228,7 @@ out:
218228
}
219229
}
220230
}
221-
glog.V(logger.Detail).Infoln("Download hashes: done. Took", time.Since(start))
231+
glog.V(logger.Detail).Infof("Downloaded hashes (%d). Took %v\n", d.queue.hashPool.Size(), time.Since(start))
222232

223233
return nil
224234
}
@@ -242,6 +252,10 @@ out:
242252
// from the available peers.
243253
if d.queue.hashPool.Size() > 0 {
244254
availablePeers := d.peers.get(idleState)
255+
if len(availablePeers) == 0 {
256+
glog.V(logger.Detail).Infoln("No peers available out of", len(d.peers))
257+
}
258+
245259
for _, peer := range availablePeers {
246260
// Get a possible chunk. If nil is returned no chunk
247261
// could be returned due to no hashes available.
@@ -317,21 +331,33 @@ func (d *Downloader) AddBlock(id string, block *types.Block, td *big.Int) {
317331
return
318332
}
319333

320-
glog.V(logger.Detail).Infoln("Inserting new block from:", id)
321-
d.queue.addBlock(id, block, td)
322-
334+
peer := d.peers.getPeer(id)
323335
// if the peer is in our healthy list of peers; update the td
324-
// here is a good chance to add the peer back to the list
325-
if peer := d.peers.getPeer(id); peer != nil {
326-
peer.mu.Lock()
327-
peer.td = td
328-
peer.recentHash = block.Hash()
329-
peer.mu.Unlock()
336+
// and add the block. Otherwise just ignore it
337+
if peer == nil {
338+
glog.V(logger.Detail).Infof("Ignored block from bad peer %s\n", id)
339+
return
330340
}
331341

342+
peer.mu.Lock()
343+
peer.td = td
344+
peer.recentHash = block.Hash()
345+
peer.mu.Unlock()
346+
347+
glog.V(logger.Detail).Infoln("Inserting new block from:", id)
348+
d.queue.addBlock(id, block, td)
349+
332350
// if neither go ahead to process
333351
if !(d.isFetchingHashes() || d.isDownloadingBlocks()) {
334-
d.process()
352+
// Check if the parent of the received block is known.
353+
// If the block is not know, request it otherwise, request.
354+
phash := block.ParentHash()
355+
if !d.hasBlock(phash) {
356+
glog.V(logger.Detail).Infof("Missing parent %x, requires fetching\n", phash.Bytes()[:4])
357+
d.syncCh <- syncPack{peer, peer.recentHash, true}
358+
} else {
359+
d.process()
360+
}
335361
}
336362
}
337363

@@ -369,7 +395,7 @@ func (d *Downloader) process() error {
369395
// TODO change this. This shite
370396
for i, block := range blocks[:max] {
371397
if !d.hasBlock(block.ParentHash()) {
372-
d.syncCh <- syncPack{d.peers.bestPeer(), block.Hash()}
398+
d.syncCh <- syncPack{d.peers.bestPeer(), block.Hash(), true}
373399
// remove processed blocks
374400
blocks = blocks[i:]
375401

0 commit comments

Comments
 (0)