@@ -263,23 +263,29 @@ func (d *Downloader) Cancel() bool {
263
263
264
264
// XXX Make synchronous
265
265
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
-
274
266
var (
267
+ start = time .Now ()
275
268
active = p // active peer will help determine the current active peer
276
269
head = common.Hash {} // common and last hash
277
270
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
279
272
attempted = make (map [string ]bool ) // attempted peers will help with retries
280
273
crossTicker = time .NewTicker (crossCheckCycle ) // ticker to periodically check expired cross checks
281
274
)
282
275
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 )
283
289
284
290
attempted [p .id ] = true
285
291
for finished := false ; ! finished ; {
@@ -293,7 +299,7 @@ func (d *Downloader) fetchHashes(p *peer, h common.Hash) error {
293
299
glog .V (logger .Debug ).Infof ("Received hashes from incorrect peer(%s)" , hashPack .peerId )
294
300
break
295
301
}
296
- timeout .Reset ( hashTTL )
302
+ timeout .Stop ( )
297
303
298
304
// Make sure the peer actually gave something valid
299
305
if len (hashPack .hashes ) == 0 {
@@ -345,7 +351,7 @@ func (d *Downloader) fetchHashes(p *peer, h common.Hash) error {
345
351
active .getBlocks ([]common.Hash {origin })
346
352
347
353
// Also fetch a fresh
348
- active . getHashes (head )
354
+ getHashes (head )
349
355
continue
350
356
}
351
357
// 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 {
399
405
// set p to the active peer. this will invalidate any hashes that may be returned
400
406
// by our previous (delayed) peer.
401
407
active = p
402
- p . getHashes (head )
408
+ getHashes (head )
403
409
glog .V (logger .Debug ).Infof ("Hash fetching switched to new peer(%s)" , p .id )
404
410
}
405
411
}
0 commit comments