github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/eth/downloader/downloader.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package downloader contains the manual full chain synchronisation.
    18  package downloader
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  	"sync"
    25  	"sync/atomic"
    26  	"time"
    27  
    28  	ethereum "github.com/FusionFoundation/efsn"
    29  	"github.com/FusionFoundation/efsn/common"
    30  	"github.com/FusionFoundation/efsn/core/rawdb"
    31  	"github.com/FusionFoundation/efsn/core/types"
    32  	"github.com/FusionFoundation/efsn/ethdb"
    33  	"github.com/FusionFoundation/efsn/event"
    34  	"github.com/FusionFoundation/efsn/log"
    35  	"github.com/FusionFoundation/efsn/metrics"
    36  	"github.com/FusionFoundation/efsn/params"
    37  )
    38  
    39  var (
    40  	MaxHashFetch    = 512 // Amount of hashes to be fetched per retrieval request
    41  	MaxBlockFetch   = 128 // Amount of blocks to be fetched per retrieval request
    42  	MaxHeaderFetch  = 192 // Amount of block headers to be fetched per retrieval request
    43  	MaxSkeletonSize = 128 // Number of header fetches to need for a skeleton assembly
    44  	MaxBodyFetch    = 128 // Amount of block bodies to be fetched per retrieval request
    45  	MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request
    46  	MaxStateFetch   = 384 // Amount of node state values to allow fetching per request
    47  
    48  	MaxForkAncestry  = 3 * params.EpochDuration // Maximum chain reorganisation
    49  	rttMinEstimate   = 2 * time.Second          // Minimum round-trip time to target for download requests
    50  	rttMaxEstimate   = 20 * time.Second         // Maximum round-trip time to target for download requests
    51  	rttMinConfidence = 0.1                      // Worse confidence factor in our estimated RTT value
    52  	ttlScaling       = 3                        // Constant scaling factor for RTT -> TTL conversion
    53  	ttlLimit         = time.Minute              // Maximum TTL allowance to prevent reaching crazy timeouts
    54  
    55  	qosTuningPeers   = 5    // Number of peers to tune based on (best peers)
    56  	qosConfidenceCap = 10   // Number of peers above which not to modify RTT confidence
    57  	qosTuningImpact  = 0.25 // Impact that a new tuning target has on the previous value
    58  
    59  	maxQueuedHeaders  = 32 * 1024 // [eth/62] Maximum number of headers to queue for import (DOS protection)
    60  	maxHeadersProcess = 2048      // Number of header download results to import at once into the chain
    61  	maxResultsProcess = 2048      // Number of content download results to import at once into the chain
    62  
    63  	fsHeaderCheckFrequency = 100             // Verification frequency of the downloaded headers during fast sync
    64  	fsHeaderSafetyNet      = 2048            // Number of headers to discard in case a chain violation is detected
    65  	fsHeaderForceVerify    = 24              // Number of headers to verify before and after the pivot to accept it
    66  	fsHeaderContCheck      = 3 * time.Second // Time interval to check for header continuations during state download
    67  	fsMinFullBlocks        = 64              // Number of blocks to retrieve fully even in fast sync
    68  )
    69  
    70  var (
    71  	errBusy                    = errors.New("busy")
    72  	errUnknownPeer             = errors.New("peer is unknown or unhealthy")
    73  	errBadPeer                 = errors.New("action from bad peer ignored")
    74  	errStallingPeer            = errors.New("peer is stalling")
    75  	errNoPeers                 = errors.New("no peers to keep download active")
    76  	errTimeout                 = errors.New("timeout")
    77  	errEmptyHeaderSet          = errors.New("empty header set by peer")
    78  	errPeersUnavailable        = errors.New("no peers available or all tried for download")
    79  	errInvalidAncestor         = errors.New("retrieved ancestor is invalid")
    80  	errInvalidChain            = errors.New("retrieved hash chain is invalid")
    81  	errInvalidBlock            = errors.New("retrieved block is invalid")
    82  	errInvalidBody             = errors.New("retrieved block body is invalid")
    83  	errInvalidReceipt          = errors.New("retrieved receipt is invalid")
    84  	errCancelBlockFetch        = errors.New("block download canceled (requested)")
    85  	errCancelHeaderFetch       = errors.New("block header download canceled (requested)")
    86  	errCancelBodyFetch         = errors.New("block body download canceled (requested)")
    87  	errCancelReceiptFetch      = errors.New("receipt download canceled (requested)")
    88  	errCancelStateFetch        = errors.New("state data download canceled (requested)")
    89  	errCancelHeaderProcessing  = errors.New("header processing canceled (requested)")
    90  	errCancelContentProcessing = errors.New("content processing canceled (requested)")
    91  	errNoSyncActive            = errors.New("no sync active")
    92  	errTooOld                  = errors.New("peer doesn't speak recent enough protocol version (need version >= 62)")
    93  )
    94  
    95  type Downloader struct {
    96  	mode SyncMode       // Synchronisation mode defining the strategy used (per sync cycle)
    97  	mux  *event.TypeMux // Event multiplexer to announce sync operation events
    98  
    99  	queue   *queue   // Scheduler for selecting the hashes to download
   100  	peers   *peerSet // Set of active peers from which download can proceed
   101  	stateDB ethdb.Database
   102  
   103  	rttEstimate   uint64 // Round trip time to target for download requests
   104  	rttConfidence uint64 // Confidence in the estimated RTT (unit: millionths to allow atomic ops)
   105  
   106  	// Statistics
   107  	syncStatsChainOrigin uint64 // Origin block number where syncing started at
   108  	syncStatsChainHeight uint64 // Highest block number known when syncing started
   109  	syncStatsState       stateSyncStats
   110  	syncStatsLock        sync.RWMutex // Lock protecting the sync stats fields
   111  
   112  	lightchain LightChain
   113  	blockchain BlockChain
   114  
   115  	// Callbacks
   116  	dropPeer peerDropFn // Drops a peer for misbehaving
   117  
   118  	// Status
   119  	synchroniseMock func(id string, hash common.Hash) error // Replacement for synchronise during testing
   120  	synchronising   int32
   121  	notified        int32
   122  	committed       int32
   123  
   124  	// Channels
   125  	headerCh      chan dataPack        // [eth/62] Channel receiving inbound block headers
   126  	bodyCh        chan dataPack        // [eth/62] Channel receiving inbound block bodies
   127  	receiptCh     chan dataPack        // [eth/63] Channel receiving inbound receipts
   128  	bodyWakeCh    chan bool            // [eth/62] Channel to signal the block body fetcher of new tasks
   129  	receiptWakeCh chan bool            // [eth/63] Channel to signal the receipt fetcher of new tasks
   130  	headerProcCh  chan []*types.Header // [eth/62] Channel to feed the header processor new tasks
   131  
   132  	// for stateFetcher
   133  	stateSyncStart chan *stateSync
   134  	trackStateReq  chan *stateReq
   135  	stateCh        chan dataPack // [eth/63] Channel receiving inbound node state data
   136  
   137  	// Cancellation and termination
   138  	cancelPeer string         // Identifier of the peer currently being used as the master (cancel on drop)
   139  	cancelCh   chan struct{}  // Channel to cancel mid-flight syncs
   140  	cancelLock sync.RWMutex   // Lock to protect the cancel channel and peer in delivers
   141  	cancelWg   sync.WaitGroup // Make sure all fetcher goroutines have exited.
   142  
   143  	quitCh   chan struct{} // Quit channel to signal termination
   144  	quitLock sync.RWMutex  // Lock to prevent double closes
   145  
   146  	// Testing hooks
   147  	syncInitHook     func(uint64, uint64)  // Method to call upon initiating a new sync run
   148  	bodyFetchHook    func([]*types.Header) // Method to call upon starting a block body fetch
   149  	receiptFetchHook func([]*types.Header) // Method to call upon starting a receipt fetch
   150  	chainInsertHook  func([]*fetchResult)  // Method to call upon inserting a chain of blocks (possibly in multiple invocations)
   151  }
   152  
   153  // LightChain encapsulates functions required to synchronise a light chain.
   154  type LightChain interface {
   155  	// HasHeader verifies a header's presence in the local chain.
   156  	HasHeader(common.Hash, uint64) bool
   157  
   158  	// GetHeaderByHash retrieves a header from the local chain.
   159  	GetHeaderByHash(common.Hash) *types.Header
   160  
   161  	// CurrentHeader retrieves the head header from the local chain.
   162  	CurrentHeader() *types.Header
   163  
   164  	// GetTd returns the total difficulty of a local block.
   165  	GetTd(common.Hash, uint64) *big.Int
   166  
   167  	// InsertHeaderChain inserts a batch of headers into the local chain.
   168  	InsertHeaderChain([]*types.Header, int) (int, error)
   169  
   170  	// Rollback removes a few recently added elements from the local chain.
   171  	Rollback([]common.Hash)
   172  }
   173  
   174  // BlockChain encapsulates functions required to sync a (full or fast) blockchain.
   175  type BlockChain interface {
   176  	LightChain
   177  
   178  	// HasBlock verifies a block's presence in the local chain.
   179  	HasBlock(common.Hash, uint64) bool
   180  
   181  	// GetBlockByHash retrieves a block from the local chain.
   182  	GetBlockByHash(common.Hash) *types.Block
   183  
   184  	// CurrentBlock retrieves the head block from the local chain.
   185  	CurrentBlock() *types.Block
   186  
   187  	// CurrentFastBlock retrieves the head fast block from the local chain.
   188  	CurrentFastBlock() *types.Block
   189  
   190  	// FastSyncCommitHead directly commits the head block to a certain entity.
   191  	FastSyncCommitHead(common.Hash) error
   192  
   193  	// InsertChain inserts a batch of blocks into the local chain.
   194  	InsertChain(types.Blocks) (int, error)
   195  
   196  	// InsertReceiptChain inserts a batch of receipts into the local chain.
   197  	InsertReceiptChain(types.Blocks, []types.Receipts) (int, error)
   198  }
   199  
   200  // New creates a new downloader to fetch hashes and blocks from remote peers.
   201  func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn) *Downloader {
   202  	if lightchain == nil {
   203  		lightchain = chain
   204  	}
   205  
   206  	dl := &Downloader{
   207  		mode:           mode,
   208  		stateDB:        stateDb,
   209  		mux:            mux,
   210  		queue:          newQueue(),
   211  		peers:          newPeerSet(),
   212  		rttEstimate:    uint64(rttMaxEstimate),
   213  		rttConfidence:  uint64(1000000),
   214  		blockchain:     chain,
   215  		lightchain:     lightchain,
   216  		dropPeer:       dropPeer,
   217  		headerCh:       make(chan dataPack, 1),
   218  		bodyCh:         make(chan dataPack, 1),
   219  		receiptCh:      make(chan dataPack, 1),
   220  		bodyWakeCh:     make(chan bool, 1),
   221  		receiptWakeCh:  make(chan bool, 1),
   222  		headerProcCh:   make(chan []*types.Header, 1),
   223  		quitCh:         make(chan struct{}),
   224  		stateCh:        make(chan dataPack),
   225  		stateSyncStart: make(chan *stateSync),
   226  		syncStatsState: stateSyncStats{
   227  			processed: rawdb.ReadFastTrieProgress(stateDb),
   228  		},
   229  		trackStateReq: make(chan *stateReq),
   230  	}
   231  	go dl.qosTuner()
   232  	go dl.stateFetcher()
   233  	return dl
   234  }
   235  
   236  // Progress retrieves the synchronisation boundaries, specifically the origin
   237  // block where synchronisation started at (may have failed/suspended); the block
   238  // or header sync is currently at; and the latest known block which the sync targets.
   239  //
   240  // In addition, during the state download phase of fast synchronisation the number
   241  // of processed and the total number of known states are also returned. Otherwise
   242  // these are zero.
   243  func (d *Downloader) Progress() ethereum.SyncProgress {
   244  	// Lock the current stats and return the progress
   245  	d.syncStatsLock.RLock()
   246  	defer d.syncStatsLock.RUnlock()
   247  
   248  	current := uint64(0)
   249  	switch d.mode {
   250  	case FullSync:
   251  		current = d.blockchain.CurrentBlock().NumberU64()
   252  	case FastSync:
   253  		current = d.blockchain.CurrentFastBlock().NumberU64()
   254  	case LightSync:
   255  		current = d.lightchain.CurrentHeader().Number.Uint64()
   256  	}
   257  	common.DebugInfo("Downloader Sync Progress", "mode", d.mode, "current", current)
   258  	return ethereum.SyncProgress{
   259  		StartingBlock: d.syncStatsChainOrigin,
   260  		CurrentBlock:  current,
   261  		HighestBlock:  d.syncStatsChainHeight,
   262  		PulledStates:  d.syncStatsState.processed,
   263  		KnownStates:   d.syncStatsState.processed + d.syncStatsState.pending,
   264  	}
   265  }
   266  
   267  // Synchronising returns whether the downloader is currently retrieving blocks.
   268  func (d *Downloader) Synchronising() bool {
   269  	return atomic.LoadInt32(&d.synchronising) > 0
   270  }
   271  
   272  // RegisterPeer injects a new download peer into the set of block source to be
   273  // used for fetching hashes and blocks from.
   274  func (d *Downloader) RegisterPeer(id string, version int, peer Peer) error {
   275  	logger := log.New("peer", id)
   276  	logger.Trace("Registering sync peer")
   277  	if err := d.peers.Register(newPeerConnection(id, version, peer, logger)); err != nil {
   278  		logger.Error("Failed to register sync peer", "err", err)
   279  		return err
   280  	}
   281  	d.qosReduceConfidence()
   282  
   283  	return nil
   284  }
   285  
   286  // RegisterLightPeer injects a light client peer, wrapping it so it appears as a regular peer.
   287  func (d *Downloader) RegisterLightPeer(id string, version int, peer LightPeer) error {
   288  	return d.RegisterPeer(id, version, &lightPeerWrapper{peer})
   289  }
   290  
   291  // UnregisterPeer remove a peer from the known list, preventing any action from
   292  // the specified peer. An effort is also made to return any pending fetches into
   293  // the queue.
   294  func (d *Downloader) UnregisterPeer(id string) error {
   295  	// Unregister the peer from the active peer set and revoke any fetch tasks
   296  	logger := log.New("peer", id)
   297  	logger.Trace("Unregistering sync peer")
   298  	if err := d.peers.Unregister(id); err != nil {
   299  		logger.Error("Failed to unregister sync peer", "err", err)
   300  		return err
   301  	}
   302  	d.queue.Revoke(id)
   303  
   304  	// If this peer was the master peer, abort sync immediately
   305  	d.cancelLock.RLock()
   306  	master := id == d.cancelPeer
   307  	d.cancelLock.RUnlock()
   308  
   309  	if master {
   310  		d.cancel()
   311  	}
   312  	return nil
   313  }
   314  
   315  // Synchronise tries to sync up our local block chain with a remote peer, both
   316  // adding various sanity checks as well as wrapping it with various log entries.
   317  func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode SyncMode) error {
   318  	err := d.synchronise(id, head, td, mode)
   319  	switch err {
   320  	case nil:
   321  	case errBusy:
   322  
   323  	case errTimeout, errBadPeer, errStallingPeer,
   324  		errEmptyHeaderSet, errPeersUnavailable, errTooOld,
   325  		errInvalidAncestor, errInvalidChain:
   326  		log.Warn("Synchronisation failed, dropping peer", "peer", id, "err", err)
   327  		if d.dropPeer == nil {
   328  			// The dropPeer method is nil when `--copydb` is used for a local copy.
   329  			// Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
   330  			log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", id)
   331  		} else {
   332  			d.dropPeer(id)
   333  		}
   334  	default:
   335  		log.Warn("Synchronisation failed, retrying", "err", err)
   336  	}
   337  	return err
   338  }
   339  
   340  // synchronise will select the peer and use it for synchronising. If an empty string is given
   341  // it will use the best peer possible and synchronize if its TD is higher than our own. If any of the
   342  // checks fail an error will be returned. This method is synchronous
   343  func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode SyncMode) error {
   344  	// Mock out the synchronisation if testing
   345  	if d.synchroniseMock != nil {
   346  		return d.synchroniseMock(id, hash)
   347  	}
   348  	// Make sure only one goroutine is ever allowed past this point at once
   349  	if !atomic.CompareAndSwapInt32(&d.synchronising, 0, 1) {
   350  		return errBusy
   351  	}
   352  	defer atomic.StoreInt32(&d.synchronising, 0)
   353  
   354  	// Post a user notification of the sync (only once per session)
   355  	if atomic.CompareAndSwapInt32(&d.notified, 0, 1) {
   356  		log.Info("Block synchronisation started")
   357  	}
   358  	// Reset the queue, peer set and wake channels to clean any internal leftover state
   359  	d.queue.Reset()
   360  	d.peers.Reset()
   361  
   362  	for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
   363  		select {
   364  		case <-ch:
   365  		default:
   366  		}
   367  	}
   368  	for _, ch := range []chan dataPack{d.headerCh, d.bodyCh, d.receiptCh} {
   369  		for empty := false; !empty; {
   370  			select {
   371  			case <-ch:
   372  			default:
   373  				empty = true
   374  			}
   375  		}
   376  	}
   377  	for empty := false; !empty; {
   378  		select {
   379  		case <-d.headerProcCh:
   380  		default:
   381  			empty = true
   382  		}
   383  	}
   384  	// Create cancel channel for aborting mid-flight and mark the master peer
   385  	d.cancelLock.Lock()
   386  	d.cancelCh = make(chan struct{})
   387  	d.cancelPeer = id
   388  	d.cancelLock.Unlock()
   389  
   390  	defer d.Cancel() // No matter what, we can't leave the cancel channel open
   391  
   392  	// Set the requested sync mode, unless it's forbidden
   393  	d.mode = mode
   394  
   395  	// Retrieve the origin peer and initiate the downloading process
   396  	p := d.peers.Peer(id)
   397  	if p == nil {
   398  		return errUnknownPeer
   399  	}
   400  	return d.syncWithPeer(p, hash, td)
   401  }
   402  
   403  // syncWithPeer starts a block synchronization based on the hash chain from the
   404  // specified peer and head hash.
   405  func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.Int) (err error) {
   406  	d.mux.Post(StartEvent{})
   407  	defer func() {
   408  		// reset on error
   409  		if err != nil {
   410  			d.mux.Post(FailedEvent{err})
   411  		} else {
   412  			d.mux.Post(DoneEvent{})
   413  		}
   414  	}()
   415  	if p.version < 62 {
   416  		return errTooOld
   417  	}
   418  
   419  	log.Debug("Synchronising with the network", "peer", p.id, "eth", p.version, "head", hash, "td", td, "mode", d.mode)
   420  	defer func(start time.Time) {
   421  		log.Debug("Synchronisation terminated", "elapsed", time.Since(start))
   422  	}(time.Now())
   423  
   424  	// Look up the sync boundaries: the common ancestor and the target block
   425  	latest, err := d.fetchHeight(p)
   426  	if err != nil {
   427  		return err
   428  	}
   429  	height := latest.Number.Uint64()
   430  
   431  	origin, err := d.findAncestor(p, height)
   432  	if err != nil {
   433  		return err
   434  	}
   435  	d.syncStatsLock.Lock()
   436  	if d.syncStatsChainHeight <= origin || d.syncStatsChainOrigin > origin {
   437  		d.syncStatsChainOrigin = origin
   438  	}
   439  	d.syncStatsChainHeight = height
   440  	d.syncStatsLock.Unlock()
   441  
   442  	// Ensure our origin point is below any fast sync pivot point
   443  	pivot := uint64(0)
   444  	if d.mode == FastSync {
   445  		if height <= uint64(fsMinFullBlocks) {
   446  			origin = 0
   447  		} else {
   448  			pivot = height - uint64(fsMinFullBlocks)
   449  			if pivot <= origin {
   450  				origin = pivot - 1
   451  			}
   452  		}
   453  	}
   454  	d.committed = 1
   455  	if d.mode == FastSync && pivot != 0 {
   456  		d.committed = 0
   457  	}
   458  	// Initiate the sync using a concurrent header and content retrieval algorithm
   459  	d.queue.Prepare(origin+1, d.mode)
   460  	if d.syncInitHook != nil {
   461  		d.syncInitHook(origin, height)
   462  	}
   463  
   464  	fetchers := []func() error{
   465  		func() error { return d.fetchHeaders(p, origin+1, pivot) }, // Headers are always retrieved
   466  		func() error { return d.fetchBodies(origin + 1) },          // Bodies are retrieved during normal and fast sync
   467  		func() error { return d.fetchReceipts(origin + 1) },        // Receipts are retrieved during fast sync
   468  		func() error { return d.processHeaders(origin+1, pivot, td) },
   469  	}
   470  	if d.mode == FastSync {
   471  		fetchers = append(fetchers, func() error { return d.processFastSyncContent(latest) })
   472  	} else if d.mode == FullSync {
   473  		fetchers = append(fetchers, d.processFullSyncContent)
   474  	}
   475  	return d.spawnSync(fetchers)
   476  }
   477  
   478  // spawnSync runs d.process and all given fetcher functions to completion in
   479  // separate goroutines, returning the first error that appears.
   480  func (d *Downloader) spawnSync(fetchers []func() error) error {
   481  	errc := make(chan error, len(fetchers))
   482  	d.cancelWg.Add(len(fetchers))
   483  	for _, fn := range fetchers {
   484  		fn := fn
   485  		go func() { defer d.cancelWg.Done(); errc <- fn() }()
   486  	}
   487  	// Wait for the first error, then terminate the others.
   488  	var err error
   489  	for i := 0; i < len(fetchers); i++ {
   490  		if i == len(fetchers)-1 {
   491  			// Close the queue when all fetchers have exited.
   492  			// This will cause the block processor to end when
   493  			// it has processed the queue.
   494  			d.queue.Close()
   495  		}
   496  		if err = <-errc; err != nil {
   497  			break
   498  		}
   499  	}
   500  	d.queue.Close()
   501  	d.Cancel()
   502  	return err
   503  }
   504  
   505  // cancel aborts all of the operations and resets the queue. However, cancel does
   506  // not wait for the running download goroutines to finish. This method should be
   507  // used when cancelling the downloads from inside the downloader.
   508  func (d *Downloader) cancel() {
   509  	// Close the current cancel channel
   510  	d.cancelLock.Lock()
   511  	if d.cancelCh != nil {
   512  		select {
   513  		case <-d.cancelCh:
   514  			// Channel was already closed
   515  		default:
   516  			close(d.cancelCh)
   517  		}
   518  	}
   519  	d.cancelLock.Unlock()
   520  }
   521  
   522  // Cancel aborts all of the operations and waits for all download goroutines to
   523  // finish before returning.
   524  func (d *Downloader) Cancel() {
   525  	d.cancel()
   526  	d.cancelWg.Wait()
   527  }
   528  
   529  // Terminate interrupts the downloader, canceling all pending operations.
   530  // The downloader cannot be reused after calling Terminate.
   531  func (d *Downloader) Terminate() {
   532  	// Close the termination channel (make sure double close is allowed)
   533  	d.quitLock.Lock()
   534  	select {
   535  	case <-d.quitCh:
   536  	default:
   537  		close(d.quitCh)
   538  	}
   539  	d.quitLock.Unlock()
   540  
   541  	// Cancel any pending download requests
   542  	d.Cancel()
   543  }
   544  
   545  // fetchHeight retrieves the head header of the remote peer to aid in estimating
   546  // the total time a pending synchronisation would take.
   547  func (d *Downloader) fetchHeight(p *peerConnection) (*types.Header, error) {
   548  	p.log.Debug("Retrieving remote chain height")
   549  
   550  	// Request the advertised remote head block and wait for the response
   551  	head, _ := p.peer.Head()
   552  	go p.peer.RequestHeadersByHash(head, 1, 0, false)
   553  
   554  	ttl := d.requestTTL()
   555  	timeout := time.After(ttl)
   556  	for {
   557  		select {
   558  		case <-d.cancelCh:
   559  			return nil, errCancelBlockFetch
   560  
   561  		case packet := <-d.headerCh:
   562  			// Discard anything not from the origin peer
   563  			if packet.PeerId() != p.id {
   564  				log.Debug("Received headers from incorrect peer", "peer", packet.PeerId())
   565  				break
   566  			}
   567  			// Make sure the peer actually gave something valid
   568  			headers := packet.(*headerPack).headers
   569  			if len(headers) != 1 {
   570  				p.log.Debug("Multiple headers for single request", "headers", len(headers))
   571  				return nil, errBadPeer
   572  			}
   573  			head := headers[0]
   574  			p.log.Debug("Remote head header identified", "number", head.Number, "hash", head.Hash())
   575  			return head, nil
   576  
   577  		case <-timeout:
   578  			p.log.Debug("Waiting for head header timed out", "elapsed", ttl)
   579  			return nil, errTimeout
   580  
   581  		case <-d.bodyCh:
   582  		case <-d.receiptCh:
   583  			// Out of bounds delivery, ignore
   584  		}
   585  	}
   586  }
   587  
   588  // findAncestor tries to locate the common ancestor link of the local chain and
   589  // a remote peers blockchain. In the general case when our node was in sync and
   590  // on the correct chain, checking the top N links should already get us a match.
   591  // In the rare scenario when we ended up on a long reorganisation (i.e. none of
   592  // the head links match), we do a binary search to find the common ancestor.
   593  func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, error) {
   594  	// Figure out the valid ancestor range to prevent rewrite attacks
   595  	floor, ceil := int64(-1), d.lightchain.CurrentHeader().Number.Uint64()
   596  
   597  	if d.mode == FullSync {
   598  		ceil = d.blockchain.CurrentBlock().NumberU64()
   599  	} else if d.mode == FastSync {
   600  		ceil = d.blockchain.CurrentFastBlock().NumberU64()
   601  	}
   602  	if ceil >= MaxForkAncestry {
   603  		floor = int64(ceil - MaxForkAncestry)
   604  	}
   605  	p.log.Debug("Looking for common ancestor", "local", ceil, "remote", height)
   606  
   607  	// Request the topmost blocks to short circuit binary ancestor lookup
   608  	head := ceil
   609  	if head > height {
   610  		head = height
   611  	}
   612  	from := int64(head) - int64(MaxHeaderFetch)
   613  	if from < 0 {
   614  		from = 0
   615  	}
   616  	// Span out with 15 block gaps into the future to catch bad head reports
   617  	limit := 2 * MaxHeaderFetch / 16
   618  	count := 1 + int((int64(ceil)-from)/16)
   619  	if count > limit {
   620  		count = limit
   621  	}
   622  	go p.peer.RequestHeadersByNumber(uint64(from), count, 15, false)
   623  
   624  	// Wait for the remote response to the head fetch
   625  	number, hash := uint64(0), common.Hash{}
   626  
   627  	ttl := d.requestTTL()
   628  	timeout := time.After(ttl)
   629  
   630  	for finished := false; !finished; {
   631  		select {
   632  		case <-d.cancelCh:
   633  			return 0, errCancelHeaderFetch
   634  
   635  		case packet := <-d.headerCh:
   636  			// Discard anything not from the origin peer
   637  			if packet.PeerId() != p.id {
   638  				log.Debug("Received headers from incorrect peer", "peer", packet.PeerId())
   639  				break
   640  			}
   641  			// Make sure the peer actually gave something valid
   642  			headers := packet.(*headerPack).headers
   643  			if len(headers) == 0 {
   644  				p.log.Warn("Empty head header set")
   645  				return 0, errEmptyHeaderSet
   646  			}
   647  			// Make sure the peer's reply conforms to the request
   648  			for i := 0; i < len(headers); i++ {
   649  				if number := headers[i].Number.Int64(); number != from+int64(i)*16 {
   650  					p.log.Warn("Head headers broke chain ordering", "index", i, "requested", from+int64(i)*16, "received", number)
   651  					return 0, errInvalidChain
   652  				}
   653  			}
   654  			// Check if a common ancestor was found
   655  			finished = true
   656  			for i := len(headers) - 1; i >= 0; i-- {
   657  				// Skip any headers that underflow/overflow our requested set
   658  				if headers[i].Number.Int64() < from || headers[i].Number.Uint64() > ceil {
   659  					continue
   660  				}
   661  				// Otherwise check if we already know the header or not
   662  				if (d.mode == FullSync && d.blockchain.HasBlock(headers[i].Hash(), headers[i].Number.Uint64())) || (d.mode != FullSync && d.lightchain.HasHeader(headers[i].Hash(), headers[i].Number.Uint64())) {
   663  					number, hash = headers[i].Number.Uint64(), headers[i].Hash()
   664  
   665  					// If every header is known, even future ones, the peer straight out lied about its head
   666  					if number > height && i == limit-1 {
   667  						p.log.Warn("Lied about chain head", "reported", height, "found", number)
   668  						return 0, errStallingPeer
   669  					}
   670  					break
   671  				}
   672  			}
   673  
   674  		case <-timeout:
   675  			p.log.Debug("Waiting for head header timed out", "elapsed", ttl)
   676  			return 0, errTimeout
   677  
   678  		case <-d.bodyCh:
   679  		case <-d.receiptCh:
   680  			// Out of bounds delivery, ignore
   681  		}
   682  	}
   683  	// If the head fetch already found an ancestor, return
   684  	if hash != (common.Hash{}) {
   685  		if int64(number) <= floor {
   686  			p.log.Warn("Ancestor below allowance", "number", number, "hash", hash, "allowance", floor)
   687  			return 0, errInvalidAncestor
   688  		}
   689  		p.log.Debug("Found common ancestor", "number", number, "hash", hash)
   690  		return number, nil
   691  	}
   692  	// Ancestor not found, we need to binary search over our chain
   693  	start, end := uint64(0), head
   694  	if floor > 0 {
   695  		start = uint64(floor)
   696  	}
   697  	for start+1 < end {
   698  		// Split our chain interval in two, and request the hash to cross check
   699  		check := (start + end) / 2
   700  
   701  		ttl := d.requestTTL()
   702  		timeout := time.After(ttl)
   703  
   704  		go p.peer.RequestHeadersByNumber(check, 1, 0, false)
   705  
   706  		// Wait until a reply arrives to this request
   707  		for arrived := false; !arrived; {
   708  			select {
   709  			case <-d.cancelCh:
   710  				return 0, errCancelHeaderFetch
   711  
   712  			case packer := <-d.headerCh:
   713  				// Discard anything not from the origin peer
   714  				if packer.PeerId() != p.id {
   715  					log.Debug("Received headers from incorrect peer", "peer", packer.PeerId())
   716  					break
   717  				}
   718  				// Make sure the peer actually gave something valid
   719  				headers := packer.(*headerPack).headers
   720  				if len(headers) != 1 {
   721  					p.log.Debug("Multiple headers for single request", "headers", len(headers))
   722  					return 0, errBadPeer
   723  				}
   724  				arrived = true
   725  
   726  				// Modify the search interval based on the response
   727  				if (d.mode == FullSync && !d.blockchain.HasBlock(headers[0].Hash(), headers[0].Number.Uint64())) || (d.mode != FullSync && !d.lightchain.HasHeader(headers[0].Hash(), headers[0].Number.Uint64())) {
   728  					end = check
   729  					break
   730  				}
   731  				header := d.lightchain.GetHeaderByHash(headers[0].Hash()) // Independent of sync mode, header surely exists
   732  				if header.Number.Uint64() != check {
   733  					p.log.Debug("Received non requested header", "number", header.Number, "hash", header.Hash(), "request", check)
   734  					return 0, errBadPeer
   735  				}
   736  				start = check
   737  
   738  			case <-timeout:
   739  				p.log.Debug("Waiting for search header timed out", "elapsed", ttl)
   740  				return 0, errTimeout
   741  
   742  			case <-d.bodyCh:
   743  			case <-d.receiptCh:
   744  				// Out of bounds delivery, ignore
   745  			}
   746  		}
   747  	}
   748  	// Ensure valid ancestry and return
   749  	if int64(start) <= floor {
   750  		p.log.Warn("Ancestor below allowance", "number", start, "hash", hash, "allowance", floor)
   751  		return 0, errInvalidAncestor
   752  	}
   753  	p.log.Debug("Found common ancestor", "number", start, "hash", hash)
   754  	return start, nil
   755  }
   756  
   757  // fetchHeaders keeps retrieving headers concurrently from the number
   758  // requested, until no more are returned, potentially throttling on the way. To
   759  // facilitate concurrency but still protect against malicious nodes sending bad
   760  // headers, we construct a header chain skeleton using the "origin" peer we are
   761  // syncing with, and fill in the missing headers using anyone else. Headers from
   762  // other peers are only accepted if they map cleanly to the skeleton. If no one
   763  // can fill in the skeleton - not even the origin peer - it's assumed invalid and
   764  // the origin is dropped.
   765  func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, pivot uint64) error {
   766  	p.log.Debug("Directing header downloads", "origin", from)
   767  	defer p.log.Debug("Header download terminated")
   768  
   769  	// Create a timeout timer, and the associated header fetcher
   770  	skeleton := true            // Skeleton assembly phase or finishing up
   771  	request := time.Now()       // time of the last skeleton fetch request
   772  	timeout := time.NewTimer(0) // timer to dump a non-responsive active peer
   773  	<-timeout.C                 // timeout channel should be initially empty
   774  	defer timeout.Stop()
   775  
   776  	var ttl time.Duration
   777  	getHeaders := func(from uint64) {
   778  		request = time.Now()
   779  
   780  		ttl = d.requestTTL()
   781  		timeout.Reset(ttl)
   782  
   783  		if skeleton {
   784  			p.log.Trace("Fetching skeleton headers", "count", MaxHeaderFetch, "from", from)
   785  			go p.peer.RequestHeadersByNumber(from+uint64(MaxHeaderFetch)-1, MaxSkeletonSize, MaxHeaderFetch-1, false)
   786  		} else {
   787  			p.log.Trace("Fetching full headers", "count", MaxHeaderFetch, "from", from)
   788  			go p.peer.RequestHeadersByNumber(from, MaxHeaderFetch, 0, false)
   789  		}
   790  	}
   791  	// Start pulling the header chain skeleton until all is done
   792  	getHeaders(from)
   793  
   794  	for {
   795  		select {
   796  		case <-d.cancelCh:
   797  			return errCancelHeaderFetch
   798  
   799  		case packet := <-d.headerCh:
   800  			// Make sure the active peer is giving us the skeleton headers
   801  			if packet.PeerId() != p.id {
   802  				log.Debug("Received skeleton from incorrect peer", "peer", packet.PeerId())
   803  				break
   804  			}
   805  			headerReqTimer.UpdateSince(request)
   806  			timeout.Stop()
   807  
   808  			// If the skeleton's finished, pull any remaining head headers directly from the origin
   809  			if packet.Items() == 0 && skeleton {
   810  				skeleton = false
   811  				getHeaders(from)
   812  				continue
   813  			}
   814  			// If no more headers are inbound, notify the content fetchers and return
   815  			if packet.Items() == 0 {
   816  				// Don't abort header fetches while the pivot is downloading
   817  				if atomic.LoadInt32(&d.committed) == 0 && pivot <= from {
   818  					p.log.Debug("No headers, waiting for pivot commit")
   819  					select {
   820  					case <-time.After(fsHeaderContCheck):
   821  						getHeaders(from)
   822  						continue
   823  					case <-d.cancelCh:
   824  						return errCancelHeaderFetch
   825  					}
   826  				}
   827  				// Pivot done (or not in fast sync) and no more headers, terminate the process
   828  				p.log.Debug("No more headers available")
   829  				select {
   830  				case d.headerProcCh <- nil:
   831  					return nil
   832  				case <-d.cancelCh:
   833  					return errCancelHeaderFetch
   834  				}
   835  			}
   836  			headers := packet.(*headerPack).headers
   837  
   838  			// If we received a skeleton batch, resolve internals concurrently
   839  			if skeleton {
   840  				filled, proced, err := d.fillHeaderSkeleton(from, headers)
   841  				if err != nil {
   842  					p.log.Warn("Skeleton chain invalid", "err", err)
   843  					return errInvalidChain
   844  				}
   845  				headers = filled[proced:]
   846  				from += uint64(proced)
   847  			}
   848  			// Insert all the new headers and fetch the next batch
   849  			if len(headers) > 0 {
   850  				p.log.Trace("Scheduling new headers", "count", len(headers), "from", from)
   851  				select {
   852  				case d.headerProcCh <- headers:
   853  				case <-d.cancelCh:
   854  					return errCancelHeaderFetch
   855  				}
   856  				from += uint64(len(headers))
   857  			}
   858  			getHeaders(from)
   859  
   860  		case <-timeout.C:
   861  			if d.dropPeer == nil {
   862  				// The dropPeer method is nil when `--copydb` is used for a local copy.
   863  				// Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
   864  				p.log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", p.id)
   865  				break
   866  			}
   867  			// Header retrieval timed out, consider the peer bad and drop
   868  			p.log.Debug("Header request timed out", "elapsed", ttl)
   869  			headerTimeoutMeter.Mark(1)
   870  			d.dropPeer(p.id)
   871  
   872  			// Finish the sync gracefully instead of dumping the gathered data though
   873  			for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
   874  				select {
   875  				case ch <- false:
   876  				case <-d.cancelCh:
   877  				}
   878  			}
   879  			select {
   880  			case d.headerProcCh <- nil:
   881  			case <-d.cancelCh:
   882  			}
   883  			return errBadPeer
   884  		}
   885  	}
   886  }
   887  
   888  // fillHeaderSkeleton concurrently retrieves headers from all our available peers
   889  // and maps them to the provided skeleton header chain.
   890  //
   891  // Any partial results from the beginning of the skeleton is (if possible) forwarded
   892  // immediately to the header processor to keep the rest of the pipeline full even
   893  // in the case of header stalls.
   894  //
   895  // The method returns the entire filled skeleton and also the number of headers
   896  // already forwarded for processing.
   897  func (d *Downloader) fillHeaderSkeleton(from uint64, skeleton []*types.Header) ([]*types.Header, int, error) {
   898  	log.Debug("Filling up skeleton", "from", from)
   899  	d.queue.ScheduleSkeleton(from, skeleton)
   900  
   901  	var (
   902  		deliver = func(packet dataPack) (int, error) {
   903  			pack := packet.(*headerPack)
   904  			return d.queue.DeliverHeaders(pack.peerID, pack.headers, d.headerProcCh)
   905  		}
   906  		expire   = func() map[string]int { return d.queue.ExpireHeaders(d.requestTTL()) }
   907  		throttle = func() bool { return false }
   908  		reserve  = func(p *peerConnection, count int) (*fetchRequest, bool, error) {
   909  			return d.queue.ReserveHeaders(p, count), false, nil
   910  		}
   911  		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchHeaders(req.From, MaxHeaderFetch) }
   912  		capacity = func(p *peerConnection) int { return p.HeaderCapacity(d.requestRTT()) }
   913  		setIdle  = func(p *peerConnection, accepted int) { p.SetHeadersIdle(accepted) }
   914  	)
   915  	err := d.fetchParts(errCancelHeaderFetch, d.headerCh, deliver, d.queue.headerContCh, expire,
   916  		d.queue.PendingHeaders, d.queue.InFlightHeaders, throttle, reserve,
   917  		nil, fetch, d.queue.CancelHeaders, capacity, d.peers.HeaderIdlePeers, setIdle, "headers")
   918  
   919  	log.Debug("Skeleton fill terminated", "err", err)
   920  
   921  	filled, proced := d.queue.RetrieveHeaders()
   922  	return filled, proced, err
   923  }
   924  
   925  // fetchBodies iteratively downloads the scheduled block bodies, taking any
   926  // available peers, reserving a chunk of blocks for each, waiting for delivery
   927  // and also periodically checking for timeouts.
   928  func (d *Downloader) fetchBodies(from uint64) error {
   929  	log.Debug("Downloading block bodies", "origin", from)
   930  
   931  	var (
   932  		deliver = func(packet dataPack) (int, error) {
   933  			pack := packet.(*bodyPack)
   934  			return d.queue.DeliverBodies(pack.peerID, pack.transactions, pack.uncles)
   935  		}
   936  		expire   = func() map[string]int { return d.queue.ExpireBodies(d.requestTTL()) }
   937  		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchBodies(req) }
   938  		capacity = func(p *peerConnection) int { return p.BlockCapacity(d.requestRTT()) }
   939  		setIdle  = func(p *peerConnection, accepted int) { p.SetBodiesIdle(accepted) }
   940  	)
   941  	err := d.fetchParts(errCancelBodyFetch, d.bodyCh, deliver, d.bodyWakeCh, expire,
   942  		d.queue.PendingBlocks, d.queue.InFlightBlocks, d.queue.ShouldThrottleBlocks, d.queue.ReserveBodies,
   943  		d.bodyFetchHook, fetch, d.queue.CancelBodies, capacity, d.peers.BodyIdlePeers, setIdle, "bodies")
   944  
   945  	log.Debug("Block body download terminated", "err", err)
   946  	return err
   947  }
   948  
   949  // fetchReceipts iteratively downloads the scheduled block receipts, taking any
   950  // available peers, reserving a chunk of receipts for each, waiting for delivery
   951  // and also periodically checking for timeouts.
   952  func (d *Downloader) fetchReceipts(from uint64) error {
   953  	log.Debug("Downloading transaction receipts", "origin", from)
   954  
   955  	var (
   956  		deliver = func(packet dataPack) (int, error) {
   957  			pack := packet.(*receiptPack)
   958  			return d.queue.DeliverReceipts(pack.peerID, pack.receipts)
   959  		}
   960  		expire   = func() map[string]int { return d.queue.ExpireReceipts(d.requestTTL()) }
   961  		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchReceipts(req) }
   962  		capacity = func(p *peerConnection) int { return p.ReceiptCapacity(d.requestRTT()) }
   963  		setIdle  = func(p *peerConnection, accepted int) { p.SetReceiptsIdle(accepted) }
   964  	)
   965  	err := d.fetchParts(errCancelReceiptFetch, d.receiptCh, deliver, d.receiptWakeCh, expire,
   966  		d.queue.PendingReceipts, d.queue.InFlightReceipts, d.queue.ShouldThrottleReceipts, d.queue.ReserveReceipts,
   967  		d.receiptFetchHook, fetch, d.queue.CancelReceipts, capacity, d.peers.ReceiptIdlePeers, setIdle, "receipts")
   968  
   969  	log.Debug("Transaction receipt download terminated", "err", err)
   970  	return err
   971  }
   972  
   973  // fetchParts iteratively downloads scheduled block parts, taking any available
   974  // peers, reserving a chunk of fetch requests for each, waiting for delivery and
   975  // also periodically checking for timeouts.
   976  //
   977  // As the scheduling/timeout logic mostly is the same for all downloaded data
   978  // types, this method is used by each for data gathering and is instrumented with
   979  // various callbacks to handle the slight differences between processing them.
   980  //
   981  // The instrumentation parameters:
   982  //  - errCancel:   error type to return if the fetch operation is cancelled (mostly makes logging nicer)
   983  //  - deliveryCh:  channel from which to retrieve downloaded data packets (merged from all concurrent peers)
   984  //  - deliver:     processing callback to deliver data packets into type specific download queues (usually within `queue`)
   985  //  - wakeCh:      notification channel for waking the fetcher when new tasks are available (or sync completed)
   986  //  - expire:      task callback method to abort requests that took too long and return the faulty peers (traffic shaping)
   987  //  - pending:     task callback for the number of requests still needing download (detect completion/non-completability)
   988  //  - inFlight:    task callback for the number of in-progress requests (wait for all active downloads to finish)
   989  //  - throttle:    task callback to check if the processing queue is full and activate throttling (bound memory use)
   990  //  - reserve:     task callback to reserve new download tasks to a particular peer (also signals partial completions)
   991  //  - fetchHook:   tester callback to notify of new tasks being initiated (allows testing the scheduling logic)
   992  //  - fetch:       network callback to actually send a particular download request to a physical remote peer
   993  //  - cancel:      task callback to abort an in-flight download request and allow rescheduling it (in case of lost peer)
   994  //  - capacity:    network callback to retrieve the estimated type-specific bandwidth capacity of a peer (traffic shaping)
   995  //  - idle:        network callback to retrieve the currently (type specific) idle peers that can be assigned tasks
   996  //  - setIdle:     network callback to set a peer back to idle and update its estimated capacity (traffic shaping)
   997  //  - kind:        textual label of the type being downloaded to display in log mesages
   998  func (d *Downloader) fetchParts(errCancel error, deliveryCh chan dataPack, deliver func(dataPack) (int, error), wakeCh chan bool,
   999  	expire func() map[string]int, pending func() int, inFlight func() bool, throttle func() bool, reserve func(*peerConnection, int) (*fetchRequest, bool, error),
  1000  	fetchHook func([]*types.Header), fetch func(*peerConnection, *fetchRequest) error, cancel func(*fetchRequest), capacity func(*peerConnection) int,
  1001  	idle func() ([]*peerConnection, int), setIdle func(*peerConnection, int), kind string) error {
  1002  
  1003  	// Create a ticker to detect expired retrieval tasks
  1004  	ticker := time.NewTicker(100 * time.Millisecond)
  1005  	defer ticker.Stop()
  1006  
  1007  	update := make(chan struct{}, 1)
  1008  
  1009  	// Prepare the queue and fetch block parts until the block header fetcher's done
  1010  	finished := false
  1011  	for {
  1012  		select {
  1013  		case <-d.cancelCh:
  1014  			return errCancel
  1015  
  1016  		case packet := <-deliveryCh:
  1017  			// If the peer was previously banned and failed to deliver its pack
  1018  			// in a reasonable time frame, ignore its message.
  1019  			if peer := d.peers.Peer(packet.PeerId()); peer != nil {
  1020  				// Deliver the received chunk of data and check chain validity
  1021  				accepted, err := deliver(packet)
  1022  				if err == errInvalidChain {
  1023  					return err
  1024  				}
  1025  				// Unless a peer delivered something completely else than requested (usually
  1026  				// caused by a timed out request which came through in the end), set it to
  1027  				// idle. If the delivery's stale, the peer should have already been idled.
  1028  				if err != errStaleDelivery {
  1029  					setIdle(peer, accepted)
  1030  				}
  1031  				// Issue a log to the user to see what's going on
  1032  				switch {
  1033  				case err == nil && packet.Items() == 0:
  1034  					peer.log.Trace("Requested data not delivered", "type", kind)
  1035  				case err == nil:
  1036  					peer.log.Trace("Delivered new batch of data", "type", kind, "count", packet.Stats())
  1037  				default:
  1038  					peer.log.Trace("Failed to deliver retrieved data", "type", kind, "err", err)
  1039  				}
  1040  			}
  1041  			// Blocks assembled, try to update the progress
  1042  			select {
  1043  			case update <- struct{}{}:
  1044  			default:
  1045  			}
  1046  
  1047  		case cont := <-wakeCh:
  1048  			// The header fetcher sent a continuation flag, check if it's done
  1049  			if !cont {
  1050  				finished = true
  1051  			}
  1052  			// Headers arrive, try to update the progress
  1053  			select {
  1054  			case update <- struct{}{}:
  1055  			default:
  1056  			}
  1057  
  1058  		case <-ticker.C:
  1059  			// Sanity check update the progress
  1060  			select {
  1061  			case update <- struct{}{}:
  1062  			default:
  1063  			}
  1064  
  1065  		case <-update:
  1066  			// Short circuit if we lost all our peers
  1067  			if d.peers.Len() == 0 {
  1068  				return errNoPeers
  1069  			}
  1070  			// Check for fetch request timeouts and demote the responsible peers
  1071  			for pid, fails := range expire() {
  1072  				if peer := d.peers.Peer(pid); peer != nil {
  1073  					// If a lot of retrieval elements expired, we might have overestimated the remote peer or perhaps
  1074  					// ourselves. Only reset to minimal throughput but don't drop just yet. If even the minimal times
  1075  					// out that sync wise we need to get rid of the peer.
  1076  					//
  1077  					// The reason the minimum threshold is 2 is because the downloader tries to estimate the bandwidth
  1078  					// and latency of a peer separately, which requires pushing the measures capacity a bit and seeing
  1079  					// how response times reacts, to it always requests one more than the minimum (i.e. min 2).
  1080  					if fails > 2 {
  1081  						peer.log.Trace("Data delivery timed out", "type", kind)
  1082  						setIdle(peer, 0)
  1083  					} else {
  1084  						peer.log.Debug("Stalling delivery, dropping", "type", kind)
  1085  						if d.dropPeer == nil {
  1086  							// The dropPeer method is nil when `--copydb` is used for a local copy.
  1087  							// Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
  1088  							peer.log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", pid)
  1089  						} else {
  1090  							d.dropPeer(pid)
  1091  						}
  1092  					}
  1093  				}
  1094  			}
  1095  			// If there's nothing more to fetch, wait or terminate
  1096  			if pending() == 0 {
  1097  				if !inFlight() && finished {
  1098  					log.Debug("Data fetching completed", "type", kind)
  1099  					return nil
  1100  				}
  1101  				break
  1102  			}
  1103  			// Send a download request to all idle peers, until throttled
  1104  			progressed, throttled, running := false, false, inFlight()
  1105  			idles, total := idle()
  1106  
  1107  			for _, peer := range idles {
  1108  				// Short circuit if throttling activated
  1109  				if throttle() {
  1110  					throttled = true
  1111  					break
  1112  				}
  1113  				// Short circuit if there is no more available task.
  1114  				if pending() == 0 {
  1115  					break
  1116  				}
  1117  				// Reserve a chunk of fetches for a peer. A nil can mean either that
  1118  				// no more headers are available, or that the peer is known not to
  1119  				// have them.
  1120  				request, progress, err := reserve(peer, capacity(peer))
  1121  				if err != nil {
  1122  					return err
  1123  				}
  1124  				if progress {
  1125  					progressed = true
  1126  				}
  1127  				if request == nil {
  1128  					continue
  1129  				}
  1130  				if request.From > 0 {
  1131  					peer.log.Trace("Requesting new batch of data", "type", kind, "from", request.From)
  1132  				} else {
  1133  					peer.log.Trace("Requesting new batch of data", "type", kind, "count", len(request.Headers), "from", request.Headers[0].Number)
  1134  				}
  1135  				// Fetch the chunk and make sure any errors return the hashes to the queue
  1136  				if fetchHook != nil {
  1137  					fetchHook(request.Headers)
  1138  				}
  1139  				if err := fetch(peer, request); err != nil {
  1140  					// Although we could try and make an attempt to fix this, this error really
  1141  					// means that we've double allocated a fetch task to a peer. If that is the
  1142  					// case, the internal state of the downloader and the queue is very wrong so
  1143  					// better hard crash and note the error instead of silently accumulating into
  1144  					// a much bigger issue.
  1145  					panic(fmt.Sprintf("%v: %s fetch assignment failed", peer, kind))
  1146  				}
  1147  				running = true
  1148  			}
  1149  			// Make sure that we have peers available for fetching. If all peers have been tried
  1150  			// and all failed throw an error
  1151  			if !progressed && !throttled && !running && len(idles) == total && pending() > 0 {
  1152  				return errPeersUnavailable
  1153  			}
  1154  		}
  1155  	}
  1156  }
  1157  
  1158  // processHeaders takes batches of retrieved headers from an input channel and
  1159  // keeps processing and scheduling them into the header chain and downloader's
  1160  // queue until the stream ends or a failure occurs.
  1161  func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) error {
  1162  	// Keep a count of uncertain headers to roll back
  1163  	rollback := []*types.Header{}
  1164  	defer func() {
  1165  		if len(rollback) > 0 {
  1166  			// Flatten the headers and roll them back
  1167  			hashes := make([]common.Hash, len(rollback))
  1168  			for i, header := range rollback {
  1169  				hashes[i] = header.Hash()
  1170  			}
  1171  			lastHeader, lastFastBlock, lastBlock := d.lightchain.CurrentHeader().Number, common.Big0, common.Big0
  1172  			if d.mode != LightSync {
  1173  				lastFastBlock = d.blockchain.CurrentFastBlock().Number()
  1174  				lastBlock = d.blockchain.CurrentBlock().Number()
  1175  			}
  1176  			d.lightchain.Rollback(hashes)
  1177  			curFastBlock, curBlock := common.Big0, common.Big0
  1178  			if d.mode != LightSync {
  1179  				curFastBlock = d.blockchain.CurrentFastBlock().Number()
  1180  				curBlock = d.blockchain.CurrentBlock().Number()
  1181  			}
  1182  			log.Warn("Rolled back headers", "count", len(hashes),
  1183  				"header", fmt.Sprintf("%d->%d", lastHeader, d.lightchain.CurrentHeader().Number),
  1184  				"fast", fmt.Sprintf("%d->%d", lastFastBlock, curFastBlock),
  1185  				"block", fmt.Sprintf("%d->%d", lastBlock, curBlock))
  1186  		}
  1187  	}()
  1188  
  1189  	// Wait for batches of headers to process
  1190  	gotHeaders := false
  1191  
  1192  	for {
  1193  		select {
  1194  		case <-d.cancelCh:
  1195  			return errCancelHeaderProcessing
  1196  
  1197  		case headers := <-d.headerProcCh:
  1198  			// Terminate header processing if we synced up
  1199  			if len(headers) == 0 {
  1200  				// Notify everyone that headers are fully processed
  1201  				for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
  1202  					select {
  1203  					case ch <- false:
  1204  					case <-d.cancelCh:
  1205  					}
  1206  				}
  1207  				// If no headers were retrieved at all, the peer violated its TD promise that it had a
  1208  				// better chain compared to ours. The only exception is if its promised blocks were
  1209  				// already imported by other means (e.g. fecher):
  1210  				//
  1211  				// R <remote peer>, L <local node>: Both at block 10
  1212  				// R: Mine block 11, and propagate it to L
  1213  				// L: Queue block 11 for import
  1214  				// L: Notice that R's head and TD increased compared to ours, start sync
  1215  				// L: Import of block 11 finishes
  1216  				// L: Sync begins, and finds common ancestor at 11
  1217  				// L: Request new headers up from 11 (R's TD was higher, it must have something)
  1218  				// R: Nothing to give
  1219  				if d.mode != LightSync {
  1220  					head := d.blockchain.CurrentBlock()
  1221  					if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 {
  1222  						return errStallingPeer
  1223  					}
  1224  				}
  1225  				// If fast or light syncing, ensure promised headers are indeed delivered. This is
  1226  				// needed to detect scenarios where an attacker feeds a bad pivot and then bails out
  1227  				// of delivering the post-pivot blocks that would flag the invalid content.
  1228  				//
  1229  				// This check cannot be executed "as is" for full imports, since blocks may still be
  1230  				// queued for processing when the header download completes. However, as long as the
  1231  				// peer gave us something useful, we're already happy/progressed (above check).
  1232  				if d.mode == FastSync || d.mode == LightSync {
  1233  					head := d.lightchain.CurrentHeader()
  1234  					if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 {
  1235  						return errStallingPeer
  1236  					}
  1237  				}
  1238  				// Disable any rollback and return
  1239  				rollback = nil
  1240  				return nil
  1241  			}
  1242  			// Otherwise split the chunk of headers into batches and process them
  1243  			gotHeaders = true
  1244  
  1245  			for len(headers) > 0 {
  1246  				// Terminate if something failed in between processing chunks
  1247  				select {
  1248  				case <-d.cancelCh:
  1249  					return errCancelHeaderProcessing
  1250  				default:
  1251  				}
  1252  				// Select the next chunk of headers to import
  1253  				limit := maxHeadersProcess
  1254  				if limit > len(headers) {
  1255  					limit = len(headers)
  1256  				}
  1257  				chunk := headers[:limit]
  1258  
  1259  				// In case of header only syncing, validate the chunk immediately
  1260  				if d.mode == FastSync || d.mode == LightSync {
  1261  					// Collect the yet unknown headers to mark them as uncertain
  1262  					unknown := make([]*types.Header, 0, len(headers))
  1263  					for _, header := range chunk {
  1264  						if !d.lightchain.HasHeader(header.Hash(), header.Number.Uint64()) {
  1265  							unknown = append(unknown, header)
  1266  						}
  1267  					}
  1268  					// If we're importing pure headers, verify based on their recentness
  1269  					frequency := fsHeaderCheckFrequency
  1270  					if chunk[len(chunk)-1].Number.Uint64()+uint64(fsHeaderForceVerify) > pivot {
  1271  						frequency = 1
  1272  					}
  1273  					if n, err := d.lightchain.InsertHeaderChain(chunk, frequency); err != nil {
  1274  						// If some headers were inserted, add them too to the rollback list
  1275  						if n > 0 {
  1276  							rollback = append(rollback, chunk[:n]...)
  1277  						}
  1278  						log.Info("Invalid header encountered", "number", chunk[n].Number, "hash", chunk[n].Hash(), "err", err)
  1279  						return errInvalidChain
  1280  					}
  1281  					// All verifications passed, store newly found uncertain headers
  1282  					rollback = append(rollback, unknown...)
  1283  					if len(rollback) > fsHeaderSafetyNet {
  1284  						rollback = append(rollback[:0], rollback[len(rollback)-fsHeaderSafetyNet:]...)
  1285  					}
  1286  				}
  1287  				// Unless we're doing light chains, schedule the headers for associated content retrieval
  1288  				if d.mode == FullSync || d.mode == FastSync {
  1289  					// If we've reached the allowed number of pending headers, stall a bit
  1290  					for d.queue.PendingBlocks() >= maxQueuedHeaders || d.queue.PendingReceipts() >= maxQueuedHeaders {
  1291  						select {
  1292  						case <-d.cancelCh:
  1293  							return errCancelHeaderProcessing
  1294  						case <-time.After(time.Second):
  1295  						}
  1296  					}
  1297  					// Otherwise insert the headers for content retrieval
  1298  					inserts := d.queue.Schedule(chunk, origin)
  1299  					if len(inserts) != len(chunk) {
  1300  						log.Debug("Stale headers")
  1301  						return errBadPeer
  1302  					}
  1303  				}
  1304  				headers = headers[limit:]
  1305  				origin += uint64(limit)
  1306  			}
  1307  
  1308  			// Update the highest block number we know if a higher one is found.
  1309  			d.syncStatsLock.Lock()
  1310  			if d.syncStatsChainHeight < origin {
  1311  				d.syncStatsChainHeight = origin - 1
  1312  			}
  1313  			d.syncStatsLock.Unlock()
  1314  
  1315  			// Signal the content downloaders of the availablility of new tasks
  1316  			for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
  1317  				select {
  1318  				case ch <- true:
  1319  				default:
  1320  				}
  1321  			}
  1322  		}
  1323  	}
  1324  }
  1325  
  1326  // processFullSyncContent takes fetch results from the queue and imports them into the chain.
  1327  func (d *Downloader) processFullSyncContent() error {
  1328  	for {
  1329  		results := d.queue.Results(true)
  1330  		if len(results) == 0 {
  1331  			return nil
  1332  		}
  1333  		if d.chainInsertHook != nil {
  1334  			d.chainInsertHook(results)
  1335  		}
  1336  		if err := d.importBlockResults(results); err != nil {
  1337  			return err
  1338  		}
  1339  	}
  1340  }
  1341  
  1342  func (d *Downloader) importBlockResults(results []*fetchResult) error {
  1343  	// Check for any early termination requests
  1344  	if len(results) == 0 {
  1345  		return nil
  1346  	}
  1347  	select {
  1348  	case <-d.quitCh:
  1349  		return errCancelContentProcessing
  1350  	default:
  1351  	}
  1352  	// Retrieve the a batch of results to import
  1353  	first, last := results[0].Header, results[len(results)-1].Header
  1354  	log.Debug("Inserting downloaded chain", "items", len(results),
  1355  		"firstnum", first.Number, "firsthash", first.Hash(),
  1356  		"lastnum", last.Number, "lasthash", last.Hash(),
  1357  	)
  1358  	blocks := make([]*types.Block, len(results))
  1359  	for i, result := range results {
  1360  		blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1361  	}
  1362  	if index, err := d.blockchain.InsertChain(blocks); err != nil {
  1363  		log.Info("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
  1364  		return errInvalidChain
  1365  	}
  1366  	return nil
  1367  }
  1368  
  1369  // processFastSyncContent takes fetch results from the queue and writes them to the
  1370  // database. It also controls the synchronisation of state nodes of the pivot block.
  1371  func (d *Downloader) processFastSyncContent(latest *types.Header) error {
  1372  	// Start syncing state of the reported head block. This should get us most of
  1373  	// the state of the pivot block.
  1374  	stateSync := d.syncState(latest.Root)
  1375  	defer stateSync.Cancel()
  1376  	go func() {
  1377  		if err := stateSync.Wait(); err != nil && err != errCancelStateFetch {
  1378  			d.queue.Close() // wake up WaitResults
  1379  		}
  1380  	}()
  1381  	// Figure out the ideal pivot block. Note, that this goalpost may move if the
  1382  	// sync takes long enough for the chain head to move significantly.
  1383  	pivot := uint64(0)
  1384  	if height := latest.Number.Uint64(); height > uint64(fsMinFullBlocks) {
  1385  		pivot = height - uint64(fsMinFullBlocks)
  1386  	}
  1387  	// To cater for moving pivot points, track the pivot block and subsequently
  1388  	// accumulated download results separately.
  1389  	var (
  1390  		oldPivot *fetchResult   // Locked in pivot block, might change eventually
  1391  		oldTail  []*fetchResult // Downloaded content after the pivot
  1392  	)
  1393  	for {
  1394  		// Wait for the next batch of downloaded data to be available, and if the pivot
  1395  		// block became stale, move the goalpost
  1396  		results := d.queue.Results(oldPivot == nil) // Block if we're not monitoring pivot staleness
  1397  		if len(results) == 0 {
  1398  			// If pivot sync is done, stop
  1399  			if oldPivot == nil {
  1400  				return stateSync.Cancel()
  1401  			}
  1402  			// If sync failed, stop
  1403  			select {
  1404  			case <-d.cancelCh:
  1405  				return stateSync.Cancel()
  1406  			default:
  1407  			}
  1408  		}
  1409  		if d.chainInsertHook != nil {
  1410  			d.chainInsertHook(results)
  1411  		}
  1412  		if oldPivot != nil {
  1413  			results = append(append([]*fetchResult{oldPivot}, oldTail...), results...)
  1414  		}
  1415  		// Split around the pivot block and process the two sides via fast/full sync
  1416  		if atomic.LoadInt32(&d.committed) == 0 {
  1417  			latest = results[len(results)-1].Header
  1418  			if height := latest.Number.Uint64(); height > pivot+2*uint64(fsMinFullBlocks) {
  1419  				log.Warn("Pivot became stale, moving", "old", pivot, "new", height-uint64(fsMinFullBlocks))
  1420  				pivot = height - uint64(fsMinFullBlocks)
  1421  			}
  1422  		}
  1423  		P, beforeP, afterP := splitAroundPivot(pivot, results)
  1424  		if err := d.commitFastSyncData(beforeP, stateSync); err != nil {
  1425  			return err
  1426  		}
  1427  		if P != nil {
  1428  			// If new pivot block found, cancel old state retrieval and restart
  1429  			if oldPivot != P {
  1430  				stateSync.Cancel()
  1431  
  1432  				stateSync = d.syncState(P.Header.Root)
  1433  				defer stateSync.Cancel()
  1434  				go func() {
  1435  					if err := stateSync.Wait(); err != nil && err != errCancelStateFetch {
  1436  						d.queue.Close() // wake up WaitResults
  1437  					}
  1438  				}()
  1439  				oldPivot = P
  1440  			}
  1441  			// Wait for completion, occasionally checking for pivot staleness
  1442  			select {
  1443  			case <-stateSync.done:
  1444  				if stateSync.err != nil {
  1445  					return stateSync.err
  1446  				}
  1447  				if err := d.commitPivotBlock(P); err != nil {
  1448  					return err
  1449  				}
  1450  				oldPivot = nil
  1451  
  1452  			case <-time.After(time.Second):
  1453  				oldTail = afterP
  1454  				continue
  1455  			}
  1456  		}
  1457  		// Fast sync done, pivot commit done, full import
  1458  		if err := d.importBlockResults(afterP); err != nil {
  1459  			return err
  1460  		}
  1461  	}
  1462  }
  1463  
  1464  func splitAroundPivot(pivot uint64, results []*fetchResult) (p *fetchResult, before, after []*fetchResult) {
  1465  	for _, result := range results {
  1466  		num := result.Header.Number.Uint64()
  1467  		switch {
  1468  		case num < pivot:
  1469  			before = append(before, result)
  1470  		case num == pivot:
  1471  			p = result
  1472  		default:
  1473  			after = append(after, result)
  1474  		}
  1475  	}
  1476  	return p, before, after
  1477  }
  1478  
  1479  func (d *Downloader) commitFastSyncData(results []*fetchResult, stateSync *stateSync) error {
  1480  	// Check for any early termination requests
  1481  	if len(results) == 0 {
  1482  		return nil
  1483  	}
  1484  	select {
  1485  	case <-d.quitCh:
  1486  		return errCancelContentProcessing
  1487  	case <-stateSync.done:
  1488  		if err := stateSync.Wait(); err != nil {
  1489  			return err
  1490  		}
  1491  	default:
  1492  	}
  1493  	// Retrieve the a batch of results to import
  1494  	first, last := results[0].Header, results[len(results)-1].Header
  1495  	log.Debug("Inserting fast-sync blocks", "items", len(results),
  1496  		"firstnum", first.Number, "firsthash", first.Hash(),
  1497  		"lastnumn", last.Number, "lasthash", last.Hash(),
  1498  	)
  1499  	blocks := make([]*types.Block, len(results))
  1500  	receipts := make([]types.Receipts, len(results))
  1501  	for i, result := range results {
  1502  		blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1503  		receipts[i] = result.Receipts
  1504  	}
  1505  	if index, err := d.blockchain.InsertReceiptChain(blocks, receipts); err != nil {
  1506  		log.Info("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
  1507  		return errInvalidChain
  1508  	}
  1509  	return nil
  1510  }
  1511  
  1512  func (d *Downloader) commitPivotBlock(result *fetchResult) error {
  1513  	block := types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1514  	log.Debug("Committing fast sync pivot as new head", "number", block.Number(), "hash", block.Hash())
  1515  	if _, err := d.blockchain.InsertReceiptChain([]*types.Block{block}, []types.Receipts{result.Receipts}); err != nil {
  1516  		return err
  1517  	}
  1518  	if err := d.blockchain.FastSyncCommitHead(block.Hash()); err != nil {
  1519  		return err
  1520  	}
  1521  	atomic.StoreInt32(&d.committed, 1)
  1522  	return nil
  1523  }
  1524  
  1525  // DeliverHeaders injects a new batch of block headers received from a remote
  1526  // node into the download schedule.
  1527  func (d *Downloader) DeliverHeaders(id string, headers []*types.Header) (err error) {
  1528  	return d.deliver(id, d.headerCh, &headerPack{id, headers}, headerInMeter, headerDropMeter)
  1529  }
  1530  
  1531  // DeliverBodies injects a new batch of block bodies received from a remote node.
  1532  func (d *Downloader) DeliverBodies(id string, transactions [][]*types.Transaction, uncles [][]*types.Header) (err error) {
  1533  	return d.deliver(id, d.bodyCh, &bodyPack{id, transactions, uncles}, bodyInMeter, bodyDropMeter)
  1534  }
  1535  
  1536  // DeliverReceipts injects a new batch of receipts received from a remote node.
  1537  func (d *Downloader) DeliverReceipts(id string, receipts [][]*types.Receipt) (err error) {
  1538  	return d.deliver(id, d.receiptCh, &receiptPack{id, receipts}, receiptInMeter, receiptDropMeter)
  1539  }
  1540  
  1541  // DeliverNodeData injects a new batch of node state data received from a remote node.
  1542  func (d *Downloader) DeliverNodeData(id string, data [][]byte) (err error) {
  1543  	return d.deliver(id, d.stateCh, &statePack{id, data}, stateInMeter, stateDropMeter)
  1544  }
  1545  
  1546  // deliver injects a new batch of data received from a remote node.
  1547  func (d *Downloader) deliver(id string, destCh chan dataPack, packet dataPack, inMeter, dropMeter metrics.Meter) (err error) {
  1548  	// Update the delivery metrics for both good and failed deliveries
  1549  	inMeter.Mark(int64(packet.Items()))
  1550  	defer func() {
  1551  		if err != nil {
  1552  			dropMeter.Mark(int64(packet.Items()))
  1553  		}
  1554  	}()
  1555  	// Deliver or abort if the sync is canceled while queuing
  1556  	d.cancelLock.RLock()
  1557  	cancel := d.cancelCh
  1558  	d.cancelLock.RUnlock()
  1559  	if cancel == nil {
  1560  		return errNoSyncActive
  1561  	}
  1562  	select {
  1563  	case destCh <- packet:
  1564  		return nil
  1565  	case <-cancel:
  1566  		return errNoSyncActive
  1567  	}
  1568  }
  1569  
  1570  // qosTuner is the quality of service tuning loop that occasionally gathers the
  1571  // peer latency statistics and updates the estimated request round trip time.
  1572  func (d *Downloader) qosTuner() {
  1573  	for {
  1574  		// Retrieve the current median RTT and integrate into the previoust target RTT
  1575  		rtt := time.Duration((1-qosTuningImpact)*float64(atomic.LoadUint64(&d.rttEstimate)) + qosTuningImpact*float64(d.peers.medianRTT()))
  1576  		atomic.StoreUint64(&d.rttEstimate, uint64(rtt))
  1577  
  1578  		// A new RTT cycle passed, increase our confidence in the estimated RTT
  1579  		conf := atomic.LoadUint64(&d.rttConfidence)
  1580  		conf = conf + (1000000-conf)/2
  1581  		atomic.StoreUint64(&d.rttConfidence, conf)
  1582  
  1583  		// Log the new QoS values and sleep until the next RTT
  1584  		log.Debug("Recalculated downloader QoS values", "rtt", rtt, "confidence", float64(conf)/1000000.0, "ttl", d.requestTTL())
  1585  		select {
  1586  		case <-d.quitCh:
  1587  			return
  1588  		case <-time.After(rtt):
  1589  		}
  1590  	}
  1591  }
  1592  
  1593  // qosReduceConfidence is meant to be called when a new peer joins the downloader's
  1594  // peer set, needing to reduce the confidence we have in out QoS estimates.
  1595  func (d *Downloader) qosReduceConfidence() {
  1596  	// If we have a single peer, confidence is always 1
  1597  	peers := uint64(d.peers.Len())
  1598  	if peers == 0 {
  1599  		// Ensure peer connectivity races don't catch us off guard
  1600  		return
  1601  	}
  1602  	if peers == 1 {
  1603  		atomic.StoreUint64(&d.rttConfidence, 1000000)
  1604  		return
  1605  	}
  1606  	// If we have a ton of peers, don't drop confidence)
  1607  	if peers >= uint64(qosConfidenceCap) {
  1608  		return
  1609  	}
  1610  	// Otherwise drop the confidence factor
  1611  	conf := atomic.LoadUint64(&d.rttConfidence) * (peers - 1) / peers
  1612  	if float64(conf)/1000000 < rttMinConfidence {
  1613  		conf = uint64(rttMinConfidence * 1000000)
  1614  	}
  1615  	atomic.StoreUint64(&d.rttConfidence, conf)
  1616  
  1617  	rtt := time.Duration(atomic.LoadUint64(&d.rttEstimate))
  1618  	log.Debug("Relaxed downloader QoS values", "rtt", rtt, "confidence", float64(conf)/1000000.0, "ttl", d.requestTTL())
  1619  }
  1620  
  1621  // requestRTT returns the current target round trip time for a download request
  1622  // to complete in.
  1623  //
  1624  // Note, the returned RTT is .9 of the actually estimated RTT. The reason is that
  1625  // the downloader tries to adapt queries to the RTT, so multiple RTT values can
  1626  // be adapted to, but smaller ones are preferred (stabler download stream).
  1627  func (d *Downloader) requestRTT() time.Duration {
  1628  	return time.Duration(atomic.LoadUint64(&d.rttEstimate)) * 9 / 10
  1629  }
  1630  
  1631  // requestTTL returns the current timeout allowance for a single download request
  1632  // to finish under.
  1633  func (d *Downloader) requestTTL() time.Duration {
  1634  	var (
  1635  		rtt  = time.Duration(atomic.LoadUint64(&d.rttEstimate))
  1636  		conf = float64(atomic.LoadUint64(&d.rttConfidence)) / 1000000.0
  1637  	)
  1638  	ttl := time.Duration(ttlScaling) * time.Duration(float64(rtt)/conf)
  1639  	if ttl > ttlLimit {
  1640  		ttl = ttlLimit
  1641  	}
  1642  	return ttl
  1643  }