Skip to content

Commit 8dc3048

Browse files
committed
eth/downloader: fix hash fetch timeout handling
Fixes #1206
1 parent 3239aca commit 8dc3048

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

eth/downloader/downloader.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,29 @@ func (d *Downloader) Cancel() bool {
263263

264264
// XXX Make synchronous
265265
func (d *Downloader) fetchHashes(p *peer, h common.Hash) error {
266-
glog.V(logger.Debug).Infof("Downloading hashes (%x) from %s", h[:4], p.id)
267-
268-
start := time.Now()
269-
270-
// Add the hash to the queue first, and start hash retrieval
271-
d.queue.Insert([]common.Hash{h})
272-
p.getHashes(h)
273-
274266
var (
267+
start = time.Now()
275268
active = p // active peer will help determine the current active peer
276269
head = common.Hash{} // common and last hash
277270

278-
timeout = time.NewTimer(hashTTL) // timer to dump a non-responsive active peer
271+
timeout = time.NewTimer(0) // timer to dump a non-responsive active peer
279272
attempted = make(map[string]bool) // attempted peers will help with retries
280273
crossTicker = time.NewTicker(crossCheckCycle) // ticker to periodically check expired cross checks
281274
)
282275
defer crossTicker.Stop()
276+
defer timeout.Stop()
277+
278+
glog.V(logger.Debug).Infof("Downloading hashes (%x) from %s", h[:4], p.id)
279+
<-timeout.C // timeout channel should be initially empty.
280+
281+
getHashes := func(from common.Hash) {
282+
active.getHashes(from)
283+
timeout.Reset(hashTTL)
284+
}
285+
286+
// Add the hash to the queue, and start hash retrieval.
287+
d.queue.Insert([]common.Hash{h})
288+
getHashes(h)
283289

284290
attempted[p.id] = true
285291
for finished := false; !finished; {
@@ -293,7 +299,7 @@ func (d *Downloader) fetchHashes(p *peer, h common.Hash) error {
293299
glog.V(logger.Debug).Infof("Received hashes from incorrect peer(%s)", hashPack.peerId)
294300
break
295301
}
296-
timeout.Reset(hashTTL)
302+
timeout.Stop()
297303

298304
// Make sure the peer actually gave something valid
299305
if len(hashPack.hashes) == 0 {
@@ -345,7 +351,7 @@ func (d *Downloader) fetchHashes(p *peer, h common.Hash) error {
345351
active.getBlocks([]common.Hash{origin})
346352

347353
// Also fetch a fresh
348-
active.getHashes(head)
354+
getHashes(head)
349355
continue
350356
}
351357
// We're done, prepare the download cache and proceed pulling the blocks
@@ -399,7 +405,7 @@ func (d *Downloader) fetchHashes(p *peer, h common.Hash) error {
399405
// set p to the active peer. this will invalidate any hashes that may be returned
400406
// by our previous (delayed) peer.
401407
active = p
402-
p.getHashes(head)
408+
getHashes(head)
403409
glog.V(logger.Debug).Infof("Hash fetching switched to new peer(%s)", p.id)
404410
}
405411
}

0 commit comments

Comments
 (0)