downloader error fix

This commit is contained in:
AnilChinchawale 2019-03-26 14:54:16 +05:30
parent a6238b785d
commit 00c46851a2

View file

@ -90,6 +90,7 @@ var (
errCancelContentProcessing = errors.New("content processing canceled (requested)") errCancelContentProcessing = errors.New("content processing canceled (requested)")
errNoSyncActive = errors.New("no sync active") errNoSyncActive = errors.New("no sync active")
errTooOld = errors.New("peer doesn't speak recent enough protocol version (need version >= 62)") errTooOld = errors.New("peer doesn't speak recent enough protocol version (need version >= 62)")
errEnoughBlock = errors.New("downloader download enough block")
) )
type Downloader struct { type Downloader struct {
@ -421,7 +422,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
}(time.Now()) }(time.Now())
// Look up the sync boundaries: the common ancestor and the target block // Look up the sync boundaries: the common ancestor and the target block
latest, err := d.fetchHeight(p) latest, err := d.fetchHeight(p,hash)
if err != nil { if err != nil {
return err return err
} }
@ -469,7 +470,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
if d.mode == FastSync { if d.mode == FastSync {
fetchers = append(fetchers, func() error { return d.processFastSyncContent(latest) }) fetchers = append(fetchers, func() error { return d.processFastSyncContent(latest) })
} else if d.mode == FullSync { } else if d.mode == FullSync {
fetchers = append(fetchers, d.processFullSyncContent) fetchers = append(fetchers, func() error { return d.processFullSyncContent(height) })
} }
return d.spawnSync(fetchers) return d.spawnSync(fetchers)
} }
@ -500,6 +501,9 @@ func (d *Downloader) spawnSync(fetchers []func() error) error {
d.queue.Close() d.queue.Close()
d.Cancel() d.Cancel()
wg.Wait() wg.Wait()
if err == errEnoughBlock {
return nil
}
return err return err
} }
@ -537,12 +541,10 @@ func (d *Downloader) Terminate() {
// fetchHeight retrieves the head header of the remote peer to aid in estimating // fetchHeight retrieves the head header of the remote peer to aid in estimating
// the total time a pending synchronisation would take. // the total time a pending synchronisation would take.
func (d *Downloader) fetchHeight(p *peerConnection) (*types.Header, error) { func (d *Downloader) fetchHeight(p *peerConnection,hash common.Hash) (*types.Header, error) {
p.log.Debug("Retrieving remote chain height")
// Request the advertised remote head block and wait for the response // Request the advertised remote head block and wait for the response
head, _ := p.peer.Head() go p.peer.RequestHeadersByHash(hash, 1, 0, false)
go p.peer.RequestHeadersByHash(head, 1, 0, false)
ttl := d.requestTTL() ttl := d.requestTTL()
timeout := time.After(ttl) timeout := time.After(ttl)
@ -1317,7 +1319,7 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) er
} }
// processFullSyncContent takes fetch results from the queue and imports them into the chain. // processFullSyncContent takes fetch results from the queue and imports them into the chain.
func (d *Downloader) processFullSyncContent() error { func (d *Downloader) processFullSyncContent(height uint64) error {
for { for {
results := d.queue.Results(true) results := d.queue.Results(true)
if len(results) == 0 { if len(results) == 0 {
@ -1358,6 +1360,11 @@ func (d *Downloader) processFullSyncContent() error {
return err return err
} }
} }
latest := results[len(results)-1]
if latest.Header != nil && latest.Header.Number.Uint64() > height {
log.Debug("Force stop because downloading enough block", "height", height, "latest", latest.Header.Number.Uint64())
return errEnoughBlock
}
} }
} }