github.com/Night-mk/quorum@v21.1.0+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  	"github.com/ethereum/go-ethereum"
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/core/rawdb"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/ethdb"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/log"
    35  	"github.com/ethereum/go-ethereum/metrics"
    36  	"github.com/ethereum/go-ethereum/params"
    37  	"github.com/ethereum/go-ethereum/permission/core"
    38  	"github.com/ethereum/go-ethereum/trie"
    39  )
    40  
    41  var (
    42  	MaxHashFetch    = 512 // Amount of hashes to be fetched per retrieval request
    43  	MaxBlockFetch   = 128 // Amount of blocks to be fetched per retrieval request
    44  	MaxHeaderFetch  = 192 // Amount of block headers to be fetched per retrieval request
    45  	MaxSkeletonSize = 128 // Number of header fetches to need for a skeleton assembly
    46  	MaxBodyFetch    = 128 // Amount of block bodies to be fetched per retrieval request
    47  	MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request
    48  	MaxStateFetch   = 384 // Amount of node state values to allow fetching per request
    49  
    50  	rttMinEstimate   = 2 * time.Second  // Minimum round-trip time to target for download requests
    51  	rttMaxEstimate   = 20 * time.Second // Maximum round-trip time to target for download requests
    52  	rttMinConfidence = 0.1              // Worse confidence factor in our estimated RTT value
    53  	ttlScaling       = 3                // Constant scaling factor for RTT -> TTL conversion
    54  	ttlLimit         = time.Minute      // Maximum TTL allowance to prevent reaching crazy timeouts
    55  
    56  	qosTuningPeers   = 5    // Number of peers to tune based on (best peers)
    57  	qosConfidenceCap = 10   // Number of peers above which not to modify RTT confidence
    58  	qosTuningImpact  = 0.25 // Impact that a new tuning target has on the previous value
    59  
    60  	maxQueuedHeaders         = 32 * 1024                    // [eth/62] Maximum number of headers to queue for import (DOS protection)
    61  	maxHeadersProcess        = 2048                         // Number of header download results to import at once into the chain
    62  	maxResultsProcess        = 2048                         // Number of content download results to import at once into the chain
    63  	maxForkAncestry   uint64 = params.ImmutabilityThreshold // Maximum chain reorganisation (locally redeclared so tests can reduce it)
    64  
    65  	reorgProtThreshold   = 48 // Threshold number of recent blocks to disable mini reorg protection
    66  	reorgProtHeaderDelay = 2  // Number of headers to delay delivering to cover mini reorgs
    67  
    68  	fsHeaderCheckFrequency = 100             // Verification frequency of the downloaded headers during fast sync
    69  	fsHeaderSafetyNet      = 2048            // Number of headers to discard in case a chain violation is detected
    70  	fsHeaderForceVerify    = 24              // Number of headers to verify before and after the pivot to accept it
    71  	fsHeaderContCheck      = 3 * time.Second // Time interval to check for header continuations during state download
    72  	fsMinFullBlocks        = 64              // Number of blocks to retrieve fully even in fast sync
    73  )
    74  
    75  var (
    76  	errBusy                    = errors.New("busy")
    77  	errUnknownPeer             = errors.New("peer is unknown or unhealthy")
    78  	errBadPeer                 = errors.New("action from bad peer ignored")
    79  	errStallingPeer            = errors.New("peer is stalling")
    80  	errUnsyncedPeer            = errors.New("unsynced peer")
    81  	errNoPeers                 = errors.New("no peers to keep download active")
    82  	errTimeout                 = errors.New("timeout")
    83  	errEmptyHeaderSet          = errors.New("empty header set by peer")
    84  	errPeersUnavailable        = errors.New("no peers available or all tried for download")
    85  	errInvalidAncestor         = errors.New("retrieved ancestor is invalid")
    86  	errInvalidChain            = errors.New("retrieved hash chain is invalid")
    87  	errInvalidBody             = errors.New("retrieved block body is invalid")
    88  	errInvalidReceipt          = errors.New("retrieved receipt is invalid")
    89  	errCancelBlockFetch        = errors.New("block download canceled (requested)")
    90  	errCancelHeaderFetch       = errors.New("block header download canceled (requested)")
    91  	errCancelStateFetch        = errors.New("state data download canceled (requested)")
    92  	errCancelContentProcessing = errors.New("content processing canceled (requested)")
    93  	errCanceled                = errors.New("syncing canceled (requested)")
    94  	errNoSyncActive            = errors.New("no sync active")
    95  	errTooOld                  = errors.New("peer doesn't speak recent enough protocol version (need version >= 62)")
    96  )
    97  
    98  type Downloader struct {
    99  	// WARNING: The `rttEstimate` and `rttConfidence` fields are accessed atomically.
   100  	// On 32 bit platforms, only 64-bit aligned fields can be atomic. The struct is
   101  	// guaranteed to be so aligned, so take advantage of that. For more information,
   102  	// see https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
   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  	mode SyncMode       // Synchronisation mode defining the strategy used (per sync cycle)
   107  	mux  *event.TypeMux // Event multiplexer to announce sync operation events
   108  
   109  	checkpoint uint64   // Checkpoint block number to enforce head against (e.g. fast sync)
   110  	genesis    uint64   // Genesis block number to limit sync to (e.g. light client CHT)
   111  	queue      *queue   // Scheduler for selecting the hashes to download
   112  	peers      *peerSet // Set of active peers from which download can proceed
   113  
   114  	stateDB    ethdb.Database  // Database to state sync into (and deduplicate via)
   115  	stateBloom *trie.SyncBloom // Bloom filter for fast trie node existence checks
   116  
   117  	// Statistics
   118  	syncStatsChainOrigin uint64 // Origin block number where syncing started at
   119  	syncStatsChainHeight uint64 // Highest block number known when syncing started
   120  	syncStatsState       stateSyncStats
   121  	syncStatsLock        sync.RWMutex // Lock protecting the sync stats fields
   122  
   123  	lightchain LightChain
   124  	blockchain BlockChain
   125  
   126  	// Callbacks
   127  	dropPeer peerDropFn // Drops a peer for misbehaving
   128  
   129  	// Status
   130  	synchroniseMock func(id string, hash common.Hash) error // Replacement for synchronise during testing
   131  	synchronising   int32
   132  	notified        int32
   133  	committed       int32
   134  	ancientLimit    uint64 // The maximum block number which can be regarded as ancient data.
   135  
   136  	// Channels
   137  	headerCh      chan dataPack        // [eth/62] Channel receiving inbound block headers
   138  	bodyCh        chan dataPack        // [eth/62] Channel receiving inbound block bodies
   139  	receiptCh     chan dataPack        // [eth/63] Channel receiving inbound receipts
   140  	bodyWakeCh    chan bool            // [eth/62] Channel to signal the block body fetcher of new tasks
   141  	receiptWakeCh chan bool            // [eth/63] Channel to signal the receipt fetcher of new tasks
   142  	headerProcCh  chan []*types.Header // [eth/62] Channel to feed the header processor new tasks
   143  
   144  	// for stateFetcher
   145  	stateSyncStart chan *stateSync
   146  	trackStateReq  chan *stateReq
   147  	stateCh        chan dataPack // [eth/63] Channel receiving inbound node state data
   148  
   149  	// Cancellation and termination
   150  	cancelPeer string         // Identifier of the peer currently being used as the master (cancel on drop)
   151  	cancelCh   chan struct{}  // Channel to cancel mid-flight syncs
   152  	cancelLock sync.RWMutex   // Lock to protect the cancel channel and peer in delivers
   153  	cancelWg   sync.WaitGroup // Make sure all fetcher goroutines have exited.
   154  
   155  	quitCh   chan struct{} // Quit channel to signal termination
   156  	quitLock sync.RWMutex  // Lock to prevent double closes
   157  
   158  	// Testing hooks
   159  	syncInitHook     func(uint64, uint64)  // Method to call upon initiating a new sync run
   160  	bodyFetchHook    func([]*types.Header) // Method to call upon starting a block body fetch
   161  	receiptFetchHook func([]*types.Header) // Method to call upon starting a receipt fetch
   162  	chainInsertHook  func([]*fetchResult)  // Method to call upon inserting a chain of blocks (possibly in multiple invocations)
   163  }
   164  
   165  // LightChain encapsulates functions required to synchronise a light chain.
   166  type LightChain interface {
   167  	// HasHeader verifies a header's presence in the local chain.
   168  	HasHeader(common.Hash, uint64) bool
   169  
   170  	// GetHeaderByHash retrieves a header from the local chain.
   171  	GetHeaderByHash(common.Hash) *types.Header
   172  
   173  	// CurrentHeader retrieves the head header from the local chain.
   174  	CurrentHeader() *types.Header
   175  
   176  	// GetTd returns the total difficulty of a local block.
   177  	GetTd(common.Hash, uint64) *big.Int
   178  
   179  	// InsertHeaderChain inserts a batch of headers into the local chain.
   180  	InsertHeaderChain([]*types.Header, int) (int, error)
   181  
   182  	// Rollback removes a few recently added elements from the local chain.
   183  	Rollback([]common.Hash)
   184  }
   185  
   186  // BlockChain encapsulates functions required to sync a (full or fast) blockchain.
   187  type BlockChain interface {
   188  	LightChain
   189  
   190  	// HasBlock verifies a block's presence in the local chain.
   191  	HasBlock(common.Hash, uint64) bool
   192  
   193  	// HasFastBlock verifies a fast block's presence in the local chain.
   194  	HasFastBlock(common.Hash, uint64) bool
   195  
   196  	// GetBlockByHash retrieves a block from the local chain.
   197  	GetBlockByHash(common.Hash) *types.Block
   198  
   199  	// CurrentBlock retrieves the head block from the local chain.
   200  	CurrentBlock() *types.Block
   201  
   202  	// CurrentFastBlock retrieves the head fast block from the local chain.
   203  	CurrentFastBlock() *types.Block
   204  
   205  	// FastSyncCommitHead directly commits the head block to a certain entity.
   206  	FastSyncCommitHead(common.Hash) error
   207  
   208  	// InsertChain inserts a batch of blocks into the local chain.
   209  	InsertChain(types.Blocks) (int, error)
   210  
   211  	// InsertReceiptChain inserts a batch of receipts into the local chain.
   212  	InsertReceiptChain(types.Blocks, []types.Receipts, uint64) (int, error)
   213  }
   214  
   215  // New creates a new downloader to fetch hashes and blocks from remote peers.
   216  func New(checkpoint uint64, stateDb ethdb.Database, stateBloom *trie.SyncBloom, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn) *Downloader {
   217  	// reset the value of maxForkAncenstry for Quorum based
   218  	maxForkAncestry = uint64(params.GetImmutabilityThreshold())
   219  
   220  	if lightchain == nil {
   221  		lightchain = chain
   222  	}
   223  	dl := &Downloader{
   224  		stateDB:        stateDb,
   225  		stateBloom:     stateBloom,
   226  		mux:            mux,
   227  		checkpoint:     checkpoint,
   228  		queue:          newQueue(),
   229  		peers:          newPeerSet(),
   230  		rttEstimate:    uint64(rttMaxEstimate),
   231  		rttConfidence:  uint64(1000000),
   232  		blockchain:     chain,
   233  		lightchain:     lightchain,
   234  		dropPeer:       dropPeer,
   235  		headerCh:       make(chan dataPack, 1),
   236  		bodyCh:         make(chan dataPack, 1),
   237  		receiptCh:      make(chan dataPack, 1),
   238  		bodyWakeCh:     make(chan bool, 1),
   239  		receiptWakeCh:  make(chan bool, 1),
   240  		headerProcCh:   make(chan []*types.Header, 1),
   241  		quitCh:         make(chan struct{}),
   242  		stateCh:        make(chan dataPack),
   243  		stateSyncStart: make(chan *stateSync),
   244  		syncStatsState: stateSyncStats{
   245  			processed: rawdb.ReadFastTrieProgress(stateDb),
   246  		},
   247  		trackStateReq: make(chan *stateReq),
   248  	}
   249  	go dl.qosTuner()
   250  	go dl.stateFetcher()
   251  	return dl
   252  }
   253  
   254  // Progress retrieves the synchronisation boundaries, specifically the origin
   255  // block where synchronisation started at (may have failed/suspended); the block
   256  // or header sync is currently at; and the latest known block which the sync targets.
   257  //
   258  // In addition, during the state download phase of fast synchronisation the number
   259  // of processed and the total number of known states are also returned. Otherwise
   260  // these are zero.
   261  func (d *Downloader) Progress() ethereum.SyncProgress {
   262  	// Lock the current stats and return the progress
   263  	d.syncStatsLock.RLock()
   264  	defer d.syncStatsLock.RUnlock()
   265  
   266  	current := uint64(0)
   267  	switch {
   268  	case d.blockchain != nil && d.mode == FullSync:
   269  		current = d.blockchain.CurrentBlock().NumberU64()
   270  	case d.blockchain != nil && d.mode == FastSync:
   271  		current = d.blockchain.CurrentFastBlock().NumberU64()
   272  	case d.lightchain != nil:
   273  		current = d.lightchain.CurrentHeader().Number.Uint64()
   274  	default:
   275  		log.Error("Unknown downloader chain/mode combo", "light", d.lightchain != nil, "full", d.blockchain != nil, "mode", d.mode)
   276  	}
   277  	return ethereum.SyncProgress{
   278  		StartingBlock: d.syncStatsChainOrigin,
   279  		CurrentBlock:  current,
   280  		HighestBlock:  d.syncStatsChainHeight,
   281  		PulledStates:  d.syncStatsState.processed,
   282  		KnownStates:   d.syncStatsState.processed + d.syncStatsState.pending,
   283  	}
   284  }
   285  
   286  // Synchronising returns whether the downloader is currently retrieving blocks.
   287  func (d *Downloader) Synchronising() bool {
   288  	return atomic.LoadInt32(&d.synchronising) > 0
   289  }
   290  
   291  // RegisterPeer injects a new download peer into the set of block source to be
   292  // used for fetching hashes and blocks from.
   293  func (d *Downloader) RegisterPeer(id string, version int, peer Peer) error {
   294  	logger := log.New("peer", id)
   295  	logger.Trace("Registering sync peer")
   296  	if err := d.peers.Register(newPeerConnection(id, version, peer, logger)); err != nil {
   297  		logger.Error("Failed to register sync peer", "err", err)
   298  		return err
   299  	}
   300  	d.qosReduceConfidence()
   301  
   302  	return nil
   303  }
   304  
   305  // RegisterLightPeer injects a light client peer, wrapping it so it appears as a regular peer.
   306  func (d *Downloader) RegisterLightPeer(id string, version int, peer LightPeer) error {
   307  	return d.RegisterPeer(id, version, &lightPeerWrapper{peer})
   308  }
   309  
   310  // UnregisterPeer remove a peer from the known list, preventing any action from
   311  // the specified peer. An effort is also made to return any pending fetches into
   312  // the queue.
   313  func (d *Downloader) UnregisterPeer(id string) error {
   314  	// Unregister the peer from the active peer set and revoke any fetch tasks
   315  	logger := log.New("peer", id)
   316  	logger.Trace("Unregistering sync peer")
   317  	if err := d.peers.Unregister(id); err != nil {
   318  		logger.Error("Failed to unregister sync peer", "err", err)
   319  		return err
   320  	}
   321  	d.queue.Revoke(id)
   322  
   323  	return nil
   324  }
   325  
   326  // Synchronise tries to sync up our local block chain with a remote peer, both
   327  // adding various sanity checks as well as wrapping it with various log entries.
   328  func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode SyncMode) error {
   329  	err := d.synchronise(id, head, td, mode)
   330  	switch err {
   331  	case nil:
   332  	case errBusy, errCanceled:
   333  
   334  	case errTimeout, errBadPeer, errStallingPeer, errUnsyncedPeer,
   335  		errEmptyHeaderSet, errPeersUnavailable, errTooOld,
   336  		errInvalidAncestor, errInvalidChain:
   337  		log.Warn("Synchronisation failed, dropping peer", "peer", id, "err", err)
   338  		if d.dropPeer == nil {
   339  			// The dropPeer method is nil when `--copydb` is used for a local copy.
   340  			// Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
   341  			log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", id)
   342  		} else {
   343  			d.dropPeer(id)
   344  		}
   345  	default:
   346  		log.Warn("Synchronisation failed, retrying", "err", err)
   347  	}
   348  	return err
   349  }
   350  
   351  // synchronise will select the peer and use it for synchronising. If an empty string is given
   352  // it will use the best peer possible and synchronize if its TD is higher than our own. If any of the
   353  // checks fail an error will be returned. This method is synchronous
   354  func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode SyncMode) error {
   355  	// Mock out the synchronisation if testing
   356  	if d.synchroniseMock != nil {
   357  		return d.synchroniseMock(id, hash)
   358  	}
   359  	// Make sure only one goroutine is ever allowed past this point at once
   360  	if !atomic.CompareAndSwapInt32(&d.synchronising, 0, 1) {
   361  		return errBusy
   362  	}
   363  
   364  	// Quorum
   365  	// changes for permissions. added set sync status to indicate permissions that node sync has started
   366  	core.SetSyncStatus()
   367  
   368  	defer atomic.StoreInt32(&d.synchronising, 0)
   369  
   370  	// Post a user notification of the sync (only once per session)
   371  	if atomic.CompareAndSwapInt32(&d.notified, 0, 1) {
   372  		log.Info("Block synchronisation started")
   373  	}
   374  	// If we are already full syncing, but have a fast-sync bloom filter laying
   375  	// around, make sure it does't use memory any more. This is a special case
   376  	// when the user attempts to fast sync a new empty network.
   377  	if mode == FullSync && d.stateBloom != nil {
   378  		d.stateBloom.Close()
   379  	}
   380  	// Reset the queue, peer set and wake channels to clean any internal leftover state
   381  	d.queue.Reset()
   382  	d.peers.Reset()
   383  
   384  	for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
   385  		select {
   386  		case <-ch:
   387  		default:
   388  		}
   389  	}
   390  	for _, ch := range []chan dataPack{d.headerCh, d.bodyCh, d.receiptCh} {
   391  		for empty := false; !empty; {
   392  			select {
   393  			case <-ch:
   394  			default:
   395  				empty = true
   396  			}
   397  		}
   398  	}
   399  	for empty := false; !empty; {
   400  		select {
   401  		case <-d.headerProcCh:
   402  		default:
   403  			empty = true
   404  		}
   405  	}
   406  	// Create cancel channel for aborting mid-flight and mark the master peer
   407  	d.cancelLock.Lock()
   408  	d.cancelCh = make(chan struct{})
   409  	d.cancelPeer = id
   410  	d.cancelLock.Unlock()
   411  
   412  	defer d.Cancel() // No matter what, we can't leave the cancel channel open
   413  
   414  	// Set the requested sync mode, unless it's forbidden
   415  	d.mode = mode
   416  
   417  	// Retrieve the origin peer and initiate the downloading process
   418  	p := d.peers.Peer(id)
   419  	if p == nil {
   420  		return errUnknownPeer
   421  	}
   422  	if d.mode == BoundedFullSync {
   423  		return d.syncWithPeerUntil(p, hash, td)
   424  	}
   425  	return d.syncWithPeer(p, hash, td)
   426  }
   427  
   428  // syncWithPeer starts a block synchronization based on the hash chain from the
   429  // specified peer and head hash.
   430  func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.Int) (err error) {
   431  	d.mux.Post(StartEvent{})
   432  	defer func() {
   433  		// reset on error
   434  		if err != nil {
   435  			d.mux.Post(FailedEvent{err})
   436  		} else {
   437  			latest := d.lightchain.CurrentHeader()
   438  			d.mux.Post(DoneEvent{latest})
   439  		}
   440  	}()
   441  	if p.version < 62 {
   442  		return errTooOld
   443  	}
   444  
   445  	log.Debug("Synchronising with the network", "peer", p.id, "eth", p.version, "head", hash, "td", td, "mode", d.mode)
   446  	defer func(start time.Time) {
   447  		log.Debug("Synchronisation terminated", "elapsed", common.PrettyDuration(time.Since(start)))
   448  	}(time.Now())
   449  
   450  	// Look up the sync boundaries: the common ancestor and the target block
   451  	latest, err := d.fetchHeight(p)
   452  	if err != nil {
   453  		return err
   454  	}
   455  	height := latest.Number.Uint64()
   456  
   457  	origin, err := d.findAncestor(p, latest)
   458  	if err != nil {
   459  		return err
   460  	}
   461  	d.syncStatsLock.Lock()
   462  	if d.syncStatsChainHeight <= origin || d.syncStatsChainOrigin > origin {
   463  		d.syncStatsChainOrigin = origin
   464  	}
   465  	d.syncStatsChainHeight = height
   466  	d.syncStatsLock.Unlock()
   467  
   468  	// Ensure our origin point is below any fast sync pivot point
   469  	pivot := uint64(0)
   470  	if d.mode == FastSync {
   471  		if height <= uint64(fsMinFullBlocks) {
   472  			origin = 0
   473  		} else {
   474  			pivot = height - uint64(fsMinFullBlocks)
   475  			if pivot <= origin {
   476  				origin = pivot - 1
   477  			}
   478  		}
   479  	}
   480  	d.committed = 1
   481  	if d.mode == FastSync && pivot != 0 {
   482  		d.committed = 0
   483  	}
   484  	if d.mode == FastSync {
   485  		// Set the ancient data limitation.
   486  		// If we are running fast sync, all block data older than ancientLimit will be
   487  		// written to the ancient store. More recent data will be written to the active
   488  		// database and will wait for the freezer to migrate.
   489  		//
   490  		// If there is a checkpoint available, then calculate the ancientLimit through
   491  		// that. Otherwise calculate the ancient limit through the advertised height
   492  		// of the remote peer.
   493  		//
   494  		// The reason for picking checkpoint first is that a malicious peer can give us
   495  		// a fake (very high) height, forcing the ancient limit to also be very high.
   496  		// The peer would start to feed us valid blocks until head, resulting in all of
   497  		// the blocks might be written into the ancient store. A following mini-reorg
   498  		// could cause issues.
   499  		if d.checkpoint != 0 && d.checkpoint > maxForkAncestry+1 {
   500  			d.ancientLimit = d.checkpoint
   501  		} else if height > maxForkAncestry+1 {
   502  			d.ancientLimit = height - maxForkAncestry - 1
   503  		}
   504  		frozen, _ := d.stateDB.Ancients() // Ignore the error here since light client can also hit here.
   505  		// If a part of blockchain data has already been written into active store,
   506  		// disable the ancient style insertion explicitly.
   507  		if origin >= frozen && frozen != 0 {
   508  			d.ancientLimit = 0
   509  			log.Info("Disabling direct-ancient mode", "origin", origin, "ancient", frozen-1)
   510  		} else if d.ancientLimit > 0 {
   511  			log.Debug("Enabling direct-ancient mode", "ancient", d.ancientLimit)
   512  		}
   513  		// Rewind the ancient store and blockchain if reorg happens.
   514  		if origin+1 < frozen {
   515  			var hashes []common.Hash
   516  			for i := origin + 1; i < d.lightchain.CurrentHeader().Number.Uint64(); i++ {
   517  				hashes = append(hashes, rawdb.ReadCanonicalHash(d.stateDB, i))
   518  			}
   519  			d.lightchain.Rollback(hashes)
   520  		}
   521  	}
   522  	// Initiate the sync using a concurrent header and content retrieval algorithm
   523  	d.queue.Prepare(origin+1, d.mode)
   524  	if d.syncInitHook != nil {
   525  		d.syncInitHook(origin, height)
   526  	}
   527  	fetchers := []func() error{
   528  		func() error { return d.fetchHeaders(p, origin+1, pivot) }, // Headers are always retrieved
   529  		func() error { return d.fetchBodies(origin + 1) },          // Bodies are retrieved during normal and fast sync
   530  		func() error { return d.fetchReceipts(origin + 1) },        // Receipts are retrieved during fast sync
   531  		func() error { return d.processHeaders(origin+1, pivot, td) },
   532  	}
   533  	if d.mode == FastSync {
   534  		fetchers = append(fetchers, func() error { return d.processFastSyncContent(latest) })
   535  	} else if d.mode == FullSync {
   536  		fetchers = append(fetchers, d.processFullSyncContent)
   537  	}
   538  	return d.spawnSync(fetchers)
   539  }
   540  
   541  // spawnSync runs d.process and all given fetcher functions to completion in
   542  // separate goroutines, returning the first error that appears.
   543  func (d *Downloader) spawnSync(fetchers []func() error) error {
   544  	errc := make(chan error, len(fetchers))
   545  	d.cancelWg.Add(len(fetchers))
   546  	for _, fn := range fetchers {
   547  		fn := fn
   548  		go func() { defer d.cancelWg.Done(); errc <- fn() }()
   549  	}
   550  	// Wait for the first error, then terminate the others.
   551  	var err error
   552  	for i := 0; i < len(fetchers); i++ {
   553  		if i == len(fetchers)-1 {
   554  			// Close the queue when all fetchers have exited.
   555  			// This will cause the block processor to end when
   556  			// it has processed the queue.
   557  			d.queue.Close()
   558  		}
   559  		if err = <-errc; err != nil && err != errCanceled {
   560  			break
   561  		}
   562  	}
   563  	d.queue.Close()
   564  	d.Cancel()
   565  	return err
   566  }
   567  
   568  // cancel aborts all of the operations and resets the queue. However, cancel does
   569  // not wait for the running download goroutines to finish. This method should be
   570  // used when cancelling the downloads from inside the downloader.
   571  func (d *Downloader) cancel() {
   572  	// Close the current cancel channel
   573  	d.cancelLock.Lock()
   574  	if d.cancelCh != nil {
   575  		select {
   576  		case <-d.cancelCh:
   577  			// Channel was already closed
   578  		default:
   579  			close(d.cancelCh)
   580  		}
   581  	}
   582  	d.cancelLock.Unlock()
   583  }
   584  
   585  // Cancel aborts all of the operations and waits for all download goroutines to
   586  // finish before returning.
   587  func (d *Downloader) Cancel() {
   588  	d.cancel()
   589  	d.cancelWg.Wait()
   590  
   591  	d.ancientLimit = 0
   592  	log.Debug("Reset ancient limit to zero")
   593  }
   594  
   595  // Terminate interrupts the downloader, canceling all pending operations.
   596  // The downloader cannot be reused after calling Terminate.
   597  func (d *Downloader) Terminate() {
   598  	// Close the termination channel (make sure double close is allowed)
   599  	d.quitLock.Lock()
   600  	select {
   601  	case <-d.quitCh:
   602  	default:
   603  		close(d.quitCh)
   604  	}
   605  	d.quitLock.Unlock()
   606  
   607  	// Cancel any pending download requests
   608  	d.Cancel()
   609  }
   610  
   611  // fetchHeight retrieves the head header of the remote peer to aid in estimating
   612  // the total time a pending synchronisation would take.
   613  func (d *Downloader) fetchHeight(p *peerConnection) (*types.Header, error) {
   614  	p.log.Debug("Retrieving remote chain height")
   615  
   616  	// Request the advertised remote head block and wait for the response
   617  	head, _ := p.peer.Head()
   618  	go p.peer.RequestHeadersByHash(head, 1, 0, false)
   619  
   620  	ttl := d.requestTTL()
   621  	timeout := time.After(ttl)
   622  	for {
   623  		select {
   624  		case <-d.cancelCh:
   625  			return nil, errCanceled
   626  
   627  		case packet := <-d.headerCh:
   628  			// Discard anything not from the origin peer
   629  			if packet.PeerId() != p.id {
   630  				log.Debug("Received headers from incorrect peer", "peer", packet.PeerId())
   631  				break
   632  			}
   633  			// Make sure the peer actually gave something valid
   634  			headers := packet.(*headerPack).headers
   635  			if len(headers) != 1 {
   636  				p.log.Debug("Multiple headers for single request", "headers", len(headers))
   637  				return nil, errBadPeer
   638  			}
   639  			head := headers[0]
   640  			if (d.mode == FastSync || d.mode == LightSync) && head.Number.Uint64() < d.checkpoint {
   641  				p.log.Warn("Remote head below checkpoint", "number", head.Number, "hash", head.Hash())
   642  				return nil, errUnsyncedPeer
   643  			}
   644  			p.log.Debug("Remote head header identified", "number", head.Number, "hash", head.Hash())
   645  			return head, nil
   646  
   647  		case <-timeout:
   648  			p.log.Debug("Waiting for head header timed out", "elapsed", ttl)
   649  			return nil, errTimeout
   650  
   651  		case <-d.bodyCh:
   652  		case <-d.receiptCh:
   653  			// Out of bounds delivery, ignore
   654  		}
   655  	}
   656  }
   657  
   658  // calculateRequestSpan calculates what headers to request from a peer when trying to determine the
   659  // common ancestor.
   660  // It returns parameters to be used for peer.RequestHeadersByNumber:
   661  //  from - starting block number
   662  //  count - number of headers to request
   663  //  skip - number of headers to skip
   664  // and also returns 'max', the last block which is expected to be returned by the remote peers,
   665  // given the (from,count,skip)
   666  func calculateRequestSpan(remoteHeight, localHeight uint64) (int64, int, int, uint64) {
   667  	var (
   668  		from     int
   669  		count    int
   670  		MaxCount = MaxHeaderFetch / 16
   671  	)
   672  	// requestHead is the highest block that we will ask for. If requestHead is not offset,
   673  	// the highest block that we will get is 16 blocks back from head, which means we
   674  	// will fetch 14 or 15 blocks unnecessarily in the case the height difference
   675  	// between us and the peer is 1-2 blocks, which is most common
   676  	requestHead := int(remoteHeight) - 1
   677  	if requestHead < 0 {
   678  		requestHead = 0
   679  	}
   680  	// requestBottom is the lowest block we want included in the query
   681  	// Ideally, we want to include just below own head
   682  	requestBottom := int(localHeight - 1)
   683  	if requestBottom < 0 {
   684  		requestBottom = 0
   685  	}
   686  	totalSpan := requestHead - requestBottom
   687  	span := 1 + totalSpan/MaxCount
   688  	if span < 2 {
   689  		span = 2
   690  	}
   691  	if span > 16 {
   692  		span = 16
   693  	}
   694  
   695  	count = 1 + totalSpan/span
   696  	if count > MaxCount {
   697  		count = MaxCount
   698  	}
   699  	if count < 2 {
   700  		count = 2
   701  	}
   702  	from = requestHead - (count-1)*span
   703  	if from < 0 {
   704  		from = 0
   705  	}
   706  	max := from + (count-1)*span
   707  	return int64(from), count, span - 1, uint64(max)
   708  }
   709  
   710  // findAncestor tries to locate the common ancestor link of the local chain and
   711  // a remote peers blockchain. In the general case when our node was in sync and
   712  // on the correct chain, checking the top N links should already get us a match.
   713  // In the rare scenario when we ended up on a long reorganisation (i.e. none of
   714  // the head links match), we do a binary search to find the common ancestor.
   715  func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header) (uint64, error) {
   716  	// Figure out the valid ancestor range to prevent rewrite attacks
   717  	var (
   718  		floor        = int64(-1)
   719  		localHeight  uint64
   720  		remoteHeight = remoteHeader.Number.Uint64()
   721  	)
   722  	switch d.mode {
   723  	case FullSync:
   724  		localHeight = d.blockchain.CurrentBlock().NumberU64()
   725  	case FastSync:
   726  		localHeight = d.blockchain.CurrentFastBlock().NumberU64()
   727  	default:
   728  		localHeight = d.lightchain.CurrentHeader().Number.Uint64()
   729  	}
   730  	p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight)
   731  
   732  	// Recap floor value for binary search
   733  	if localHeight >= maxForkAncestry {
   734  		// We're above the max reorg threshold, find the earliest fork point
   735  		floor = int64(localHeight - maxForkAncestry)
   736  	}
   737  	// If we're doing a light sync, ensure the floor doesn't go below the CHT, as
   738  	// all headers before that point will be missing.
   739  	if d.mode == LightSync {
   740  		// If we dont know the current CHT position, find it
   741  		if d.genesis == 0 {
   742  			header := d.lightchain.CurrentHeader()
   743  			for header != nil {
   744  				d.genesis = header.Number.Uint64()
   745  				if floor >= int64(d.genesis)-1 {
   746  					break
   747  				}
   748  				header = d.lightchain.GetHeaderByHash(header.ParentHash)
   749  			}
   750  		}
   751  		// We already know the "genesis" block number, cap floor to that
   752  		if floor < int64(d.genesis)-1 {
   753  			floor = int64(d.genesis) - 1
   754  		}
   755  	}
   756  
   757  	from, count, skip, max := calculateRequestSpan(remoteHeight, localHeight)
   758  
   759  	p.log.Trace("Span searching for common ancestor", "count", count, "from", from, "skip", skip)
   760  	go p.peer.RequestHeadersByNumber(uint64(from), count, skip, false)
   761  
   762  	// Wait for the remote response to the head fetch
   763  	number, hash := uint64(0), common.Hash{}
   764  
   765  	ttl := d.requestTTL()
   766  	timeout := time.After(ttl)
   767  
   768  	for finished := false; !finished; {
   769  		select {
   770  		case <-d.cancelCh:
   771  			return 0, errCanceled
   772  
   773  		case packet := <-d.headerCh:
   774  			// Discard anything not from the origin peer
   775  			if packet.PeerId() != p.id {
   776  				log.Debug("Received headers from incorrect peer", "peer", packet.PeerId())
   777  				break
   778  			}
   779  			// Make sure the peer actually gave something valid
   780  			headers := packet.(*headerPack).headers
   781  			if len(headers) == 0 {
   782  				p.log.Warn("Empty head header set")
   783  				return 0, errEmptyHeaderSet
   784  			}
   785  			// Make sure the peer's reply conforms to the request
   786  			for i, header := range headers {
   787  				expectNumber := from + int64(i)*int64(skip+1)
   788  				if number := header.Number.Int64(); number != expectNumber {
   789  					p.log.Warn("Head headers broke chain ordering", "index", i, "requested", expectNumber, "received", number)
   790  					return 0, errInvalidChain
   791  				}
   792  			}
   793  			// Check if a common ancestor was found
   794  			finished = true
   795  			for i := len(headers) - 1; i >= 0; i-- {
   796  				// Skip any headers that underflow/overflow our requested set
   797  				if headers[i].Number.Int64() < from || headers[i].Number.Uint64() > max {
   798  					continue
   799  				}
   800  				// Otherwise check if we already know the header or not
   801  				h := headers[i].Hash()
   802  				n := headers[i].Number.Uint64()
   803  
   804  				var known bool
   805  				switch d.mode {
   806  				case FullSync:
   807  					known = d.blockchain.HasBlock(h, n)
   808  				case FastSync:
   809  					known = d.blockchain.HasFastBlock(h, n)
   810  				default:
   811  					known = d.lightchain.HasHeader(h, n)
   812  				}
   813  				if known {
   814  					number, hash = n, h
   815  					break
   816  				}
   817  			}
   818  
   819  		case <-timeout:
   820  			p.log.Debug("Waiting for head header timed out", "elapsed", ttl)
   821  			return 0, errTimeout
   822  
   823  		case <-d.bodyCh:
   824  		case <-d.receiptCh:
   825  			// Out of bounds delivery, ignore
   826  		}
   827  	}
   828  	// If the head fetch already found an ancestor, return
   829  	if hash != (common.Hash{}) {
   830  		if int64(number) <= floor {
   831  			p.log.Warn("Ancestor below allowance", "number", number, "hash", hash, "allowance", floor)
   832  			return 0, errInvalidAncestor
   833  		}
   834  		p.log.Debug("Found common ancestor", "number", number, "hash", hash)
   835  		return number, nil
   836  	}
   837  	// Ancestor not found, we need to binary search over our chain
   838  	start, end := uint64(0), remoteHeight
   839  	if floor > 0 {
   840  		start = uint64(floor)
   841  	}
   842  	p.log.Trace("Binary searching for common ancestor", "start", start, "end", end)
   843  
   844  	for start+1 < end {
   845  		// Split our chain interval in two, and request the hash to cross check
   846  		check := (start + end) / 2
   847  
   848  		ttl := d.requestTTL()
   849  		timeout := time.After(ttl)
   850  
   851  		go p.peer.RequestHeadersByNumber(check, 1, 0, false)
   852  
   853  		// Wait until a reply arrives to this request
   854  		for arrived := false; !arrived; {
   855  			select {
   856  			case <-d.cancelCh:
   857  				return 0, errCanceled
   858  
   859  			case packer := <-d.headerCh:
   860  				// Discard anything not from the origin peer
   861  				if packer.PeerId() != p.id {
   862  					log.Debug("Received headers from incorrect peer", "peer", packer.PeerId())
   863  					break
   864  				}
   865  				// Make sure the peer actually gave something valid
   866  				headers := packer.(*headerPack).headers
   867  				if len(headers) != 1 {
   868  					p.log.Debug("Multiple headers for single request", "headers", len(headers))
   869  					return 0, errBadPeer
   870  				}
   871  				arrived = true
   872  
   873  				// Modify the search interval based on the response
   874  				h := headers[0].Hash()
   875  				n := headers[0].Number.Uint64()
   876  
   877  				var known bool
   878  				switch d.mode {
   879  				case FullSync:
   880  					known = d.blockchain.HasBlock(h, n)
   881  				case FastSync:
   882  					known = d.blockchain.HasFastBlock(h, n)
   883  				default:
   884  					known = d.lightchain.HasHeader(h, n)
   885  				}
   886  				if !known {
   887  					end = check
   888  					break
   889  				}
   890  				header := d.lightchain.GetHeaderByHash(h) // Independent of sync mode, header surely exists
   891  				if header.Number.Uint64() != check {
   892  					p.log.Debug("Received non requested header", "number", header.Number, "hash", header.Hash(), "request", check)
   893  					return 0, errBadPeer
   894  				}
   895  				start = check
   896  				hash = h
   897  
   898  			case <-timeout:
   899  				p.log.Debug("Waiting for search header timed out", "elapsed", ttl)
   900  				return 0, errTimeout
   901  
   902  			case <-d.bodyCh:
   903  			case <-d.receiptCh:
   904  				// Out of bounds delivery, ignore
   905  			}
   906  		}
   907  	}
   908  	// Ensure valid ancestry and return
   909  	if int64(start) <= floor {
   910  		p.log.Warn("Ancestor below allowance", "number", start, "hash", hash, "allowance", floor)
   911  		return 0, errInvalidAncestor
   912  	}
   913  	p.log.Debug("Found common ancestor", "number", start, "hash", hash)
   914  	return start, nil
   915  }
   916  
   917  // fetchHeaders keeps retrieving headers concurrently from the number
   918  // requested, until no more are returned, potentially throttling on the way. To
   919  // facilitate concurrency but still protect against malicious nodes sending bad
   920  // headers, we construct a header chain skeleton using the "origin" peer we are
   921  // syncing with, and fill in the missing headers using anyone else. Headers from
   922  // other peers are only accepted if they map cleanly to the skeleton. If no one
   923  // can fill in the skeleton - not even the origin peer - it's assumed invalid and
   924  // the origin is dropped.
   925  func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, pivot uint64) error {
   926  	p.log.Debug("Directing header downloads", "origin", from)
   927  	defer p.log.Debug("Header download terminated")
   928  
   929  	// Create a timeout timer, and the associated header fetcher
   930  	skeleton := true            // Skeleton assembly phase or finishing up
   931  	request := time.Now()       // time of the last skeleton fetch request
   932  	timeout := time.NewTimer(0) // timer to dump a non-responsive active peer
   933  	<-timeout.C                 // timeout channel should be initially empty
   934  	defer timeout.Stop()
   935  
   936  	var ttl time.Duration
   937  	getHeaders := func(from uint64) {
   938  		request = time.Now()
   939  
   940  		ttl = d.requestTTL()
   941  		timeout.Reset(ttl)
   942  
   943  		if skeleton {
   944  			p.log.Trace("Fetching skeleton headers", "count", MaxHeaderFetch, "from", from)
   945  			go p.peer.RequestHeadersByNumber(from+uint64(MaxHeaderFetch)-1, MaxSkeletonSize, MaxHeaderFetch-1, false)
   946  		} else {
   947  			p.log.Trace("Fetching full headers", "count", MaxHeaderFetch, "from", from)
   948  			go p.peer.RequestHeadersByNumber(from, MaxHeaderFetch, 0, false)
   949  		}
   950  	}
   951  	// Start pulling the header chain skeleton until all is done
   952  	ancestor := from
   953  	getHeaders(from)
   954  
   955  	for {
   956  		select {
   957  		case <-d.cancelCh:
   958  			return errCanceled
   959  
   960  		case packet := <-d.headerCh:
   961  			// Make sure the active peer is giving us the skeleton headers
   962  			if packet.PeerId() != p.id {
   963  				log.Debug("Received skeleton from incorrect peer", "peer", packet.PeerId())
   964  				break
   965  			}
   966  			headerReqTimer.UpdateSince(request)
   967  			timeout.Stop()
   968  
   969  			// If the skeleton's finished, pull any remaining head headers directly from the origin
   970  			if packet.Items() == 0 && skeleton {
   971  				skeleton = false
   972  				getHeaders(from)
   973  				continue
   974  			}
   975  			// If no more headers are inbound, notify the content fetchers and return
   976  			if packet.Items() == 0 {
   977  				// Don't abort header fetches while the pivot is downloading
   978  				if atomic.LoadInt32(&d.committed) == 0 && pivot <= from {
   979  					p.log.Debug("No headers, waiting for pivot commit")
   980  					select {
   981  					case <-time.After(fsHeaderContCheck):
   982  						getHeaders(from)
   983  						continue
   984  					case <-d.cancelCh:
   985  						return errCanceled
   986  					}
   987  				}
   988  				// Pivot done (or not in fast sync) and no more headers, terminate the process
   989  				p.log.Debug("No more headers available")
   990  				select {
   991  				case d.headerProcCh <- nil:
   992  					return nil
   993  				case <-d.cancelCh:
   994  					return errCanceled
   995  				}
   996  			}
   997  			headers := packet.(*headerPack).headers
   998  
   999  			// If we received a skeleton batch, resolve internals concurrently
  1000  			if skeleton {
  1001  				filled, proced, err := d.fillHeaderSkeleton(from, headers)
  1002  				if err != nil {
  1003  					p.log.Debug("Skeleton chain invalid", "err", err)
  1004  					return errInvalidChain
  1005  				}
  1006  				headers = filled[proced:]
  1007  				from += uint64(proced)
  1008  			} else {
  1009  				// If we're closing in on the chain head, but haven't yet reached it, delay
  1010  				// the last few headers so mini reorgs on the head don't cause invalid hash
  1011  				// chain errors.
  1012  				if n := len(headers); n > 0 {
  1013  					// Retrieve the current head we're at
  1014  					var head uint64
  1015  					if d.mode == LightSync {
  1016  						head = d.lightchain.CurrentHeader().Number.Uint64()
  1017  					} else {
  1018  						head = d.blockchain.CurrentFastBlock().NumberU64()
  1019  						if full := d.blockchain.CurrentBlock().NumberU64(); head < full {
  1020  							head = full
  1021  						}
  1022  					}
  1023  					// If the head is below the common ancestor, we're actually deduplicating
  1024  					// already existing chain segments, so use the ancestor as the fake head.
  1025  					// Otherwise we might end up delaying header deliveries pointlessly.
  1026  					if head < ancestor {
  1027  						head = ancestor
  1028  					}
  1029  					// If the head is way older than this batch, delay the last few headers
  1030  					if head+uint64(reorgProtThreshold) < headers[n-1].Number.Uint64() {
  1031  						delay := reorgProtHeaderDelay
  1032  						if delay > n {
  1033  							delay = n
  1034  						}
  1035  						headers = headers[:n-delay]
  1036  					}
  1037  				}
  1038  			}
  1039  			// Insert all the new headers and fetch the next batch
  1040  			if len(headers) > 0 {
  1041  				p.log.Trace("Scheduling new headers", "count", len(headers), "from", from)
  1042  				select {
  1043  				case d.headerProcCh <- headers:
  1044  				case <-d.cancelCh:
  1045  					return errCanceled
  1046  				}
  1047  				from += uint64(len(headers))
  1048  				getHeaders(from)
  1049  			} else {
  1050  				// No headers delivered, or all of them being delayed, sleep a bit and retry
  1051  				p.log.Trace("All headers delayed, waiting")
  1052  				select {
  1053  				case <-time.After(fsHeaderContCheck):
  1054  					getHeaders(from)
  1055  					continue
  1056  				case <-d.cancelCh:
  1057  					return errCanceled
  1058  				}
  1059  			}
  1060  
  1061  		case <-timeout.C:
  1062  			if d.dropPeer == nil {
  1063  				// The dropPeer method is nil when `--copydb` is used for a local copy.
  1064  				// Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
  1065  				p.log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", p.id)
  1066  				break
  1067  			}
  1068  			// Header retrieval timed out, consider the peer bad and drop
  1069  			p.log.Debug("Header request timed out", "elapsed", ttl)
  1070  			headerTimeoutMeter.Mark(1)
  1071  			d.dropPeer(p.id)
  1072  
  1073  			// Finish the sync gracefully instead of dumping the gathered data though
  1074  			for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
  1075  				select {
  1076  				case ch <- false:
  1077  				case <-d.cancelCh:
  1078  				}
  1079  			}
  1080  			select {
  1081  			case d.headerProcCh <- nil:
  1082  			case <-d.cancelCh:
  1083  			}
  1084  			return errBadPeer
  1085  		}
  1086  	}
  1087  }
  1088  
  1089  // fillHeaderSkeleton concurrently retrieves headers from all our available peers
  1090  // and maps them to the provided skeleton header chain.
  1091  //
  1092  // Any partial results from the beginning of the skeleton is (if possible) forwarded
  1093  // immediately to the header processor to keep the rest of the pipeline full even
  1094  // in the case of header stalls.
  1095  //
  1096  // The method returns the entire filled skeleton and also the number of headers
  1097  // already forwarded for processing.
  1098  func (d *Downloader) fillHeaderSkeleton(from uint64, skeleton []*types.Header) ([]*types.Header, int, error) {
  1099  	log.Debug("Filling up skeleton", "from", from)
  1100  	d.queue.ScheduleSkeleton(from, skeleton)
  1101  
  1102  	var (
  1103  		deliver = func(packet dataPack) (int, error) {
  1104  			pack := packet.(*headerPack)
  1105  			return d.queue.DeliverHeaders(pack.peerID, pack.headers, d.headerProcCh)
  1106  		}
  1107  		expire   = func() map[string]int { return d.queue.ExpireHeaders(d.requestTTL()) }
  1108  		throttle = func() bool { return false }
  1109  		reserve  = func(p *peerConnection, count int) (*fetchRequest, bool, error) {
  1110  			return d.queue.ReserveHeaders(p, count), false, nil
  1111  		}
  1112  		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchHeaders(req.From, MaxHeaderFetch) }
  1113  		capacity = func(p *peerConnection) int { return p.HeaderCapacity(d.requestRTT()) }
  1114  		setIdle  = func(p *peerConnection, accepted int) { p.SetHeadersIdle(accepted) }
  1115  	)
  1116  	err := d.fetchParts(d.headerCh, deliver, d.queue.headerContCh, expire,
  1117  		d.queue.PendingHeaders, d.queue.InFlightHeaders, throttle, reserve,
  1118  		nil, fetch, d.queue.CancelHeaders, capacity, d.peers.HeaderIdlePeers, setIdle, "headers")
  1119  
  1120  	log.Debug("Skeleton fill terminated", "err", err)
  1121  
  1122  	filled, proced := d.queue.RetrieveHeaders()
  1123  	return filled, proced, err
  1124  }
  1125  
  1126  // fetchBodies iteratively downloads the scheduled block bodies, taking any
  1127  // available peers, reserving a chunk of blocks for each, waiting for delivery
  1128  // and also periodically checking for timeouts.
  1129  func (d *Downloader) fetchBodies(from uint64) error {
  1130  	log.Debug("Downloading block bodies", "origin", from)
  1131  
  1132  	var (
  1133  		deliver = func(packet dataPack) (int, error) {
  1134  			pack := packet.(*bodyPack)
  1135  			return d.queue.DeliverBodies(pack.peerID, pack.transactions, pack.uncles)
  1136  		}
  1137  		expire   = func() map[string]int { return d.queue.ExpireBodies(d.requestTTL()) }
  1138  		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchBodies(req) }
  1139  		capacity = func(p *peerConnection) int { return p.BlockCapacity(d.requestRTT()) }
  1140  		setIdle  = func(p *peerConnection, accepted int) { p.SetBodiesIdle(accepted) }
  1141  	)
  1142  	err := d.fetchParts(d.bodyCh, deliver, d.bodyWakeCh, expire,
  1143  		d.queue.PendingBlocks, d.queue.InFlightBlocks, d.queue.ShouldThrottleBlocks, d.queue.ReserveBodies,
  1144  		d.bodyFetchHook, fetch, d.queue.CancelBodies, capacity, d.peers.BodyIdlePeers, setIdle, "bodies")
  1145  
  1146  	log.Debug("Block body download terminated", "err", err)
  1147  	return err
  1148  }
  1149  
  1150  // fetchReceipts iteratively downloads the scheduled block receipts, taking any
  1151  // available peers, reserving a chunk of receipts for each, waiting for delivery
  1152  // and also periodically checking for timeouts.
  1153  func (d *Downloader) fetchReceipts(from uint64) error {
  1154  	log.Debug("Downloading transaction receipts", "origin", from)
  1155  
  1156  	var (
  1157  		deliver = func(packet dataPack) (int, error) {
  1158  			pack := packet.(*receiptPack)
  1159  			return d.queue.DeliverReceipts(pack.peerID, pack.receipts)
  1160  		}
  1161  		expire   = func() map[string]int { return d.queue.ExpireReceipts(d.requestTTL()) }
  1162  		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchReceipts(req) }
  1163  		capacity = func(p *peerConnection) int { return p.ReceiptCapacity(d.requestRTT()) }
  1164  		setIdle  = func(p *peerConnection, accepted int) { p.SetReceiptsIdle(accepted) }
  1165  	)
  1166  	err := d.fetchParts(d.receiptCh, deliver, d.receiptWakeCh, expire,
  1167  		d.queue.PendingReceipts, d.queue.InFlightReceipts, d.queue.ShouldThrottleReceipts, d.queue.ReserveReceipts,
  1168  		d.receiptFetchHook, fetch, d.queue.CancelReceipts, capacity, d.peers.ReceiptIdlePeers, setIdle, "receipts")
  1169  
  1170  	log.Debug("Transaction receipt download terminated", "err", err)
  1171  	return err
  1172  }
  1173  
  1174  // fetchParts iteratively downloads scheduled block parts, taking any available
  1175  // peers, reserving a chunk of fetch requests for each, waiting for delivery and
  1176  // also periodically checking for timeouts.
  1177  //
  1178  // As the scheduling/timeout logic mostly is the same for all downloaded data
  1179  // types, this method is used by each for data gathering and is instrumented with
  1180  // various callbacks to handle the slight differences between processing them.
  1181  //
  1182  // The instrumentation parameters:
  1183  //  - errCancel:   error type to return if the fetch operation is cancelled (mostly makes logging nicer)
  1184  //  - deliveryCh:  channel from which to retrieve downloaded data packets (merged from all concurrent peers)
  1185  //  - deliver:     processing callback to deliver data packets into type specific download queues (usually within `queue`)
  1186  //  - wakeCh:      notification channel for waking the fetcher when new tasks are available (or sync completed)
  1187  //  - expire:      task callback method to abort requests that took too long and return the faulty peers (traffic shaping)
  1188  //  - pending:     task callback for the number of requests still needing download (detect completion/non-completability)
  1189  //  - inFlight:    task callback for the number of in-progress requests (wait for all active downloads to finish)
  1190  //  - throttle:    task callback to check if the processing queue is full and activate throttling (bound memory use)
  1191  //  - reserve:     task callback to reserve new download tasks to a particular peer (also signals partial completions)
  1192  //  - fetchHook:   tester callback to notify of new tasks being initiated (allows testing the scheduling logic)
  1193  //  - fetch:       network callback to actually send a particular download request to a physical remote peer
  1194  //  - cancel:      task callback to abort an in-flight download request and allow rescheduling it (in case of lost peer)
  1195  //  - capacity:    network callback to retrieve the estimated type-specific bandwidth capacity of a peer (traffic shaping)
  1196  //  - idle:        network callback to retrieve the currently (type specific) idle peers that can be assigned tasks
  1197  //  - setIdle:     network callback to set a peer back to idle and update its estimated capacity (traffic shaping)
  1198  //  - kind:        textual label of the type being downloaded to display in log mesages
  1199  func (d *Downloader) fetchParts(deliveryCh chan dataPack, deliver func(dataPack) (int, error), wakeCh chan bool,
  1200  	expire func() map[string]int, pending func() int, inFlight func() bool, throttle func() bool, reserve func(*peerConnection, int) (*fetchRequest, bool, error),
  1201  	fetchHook func([]*types.Header), fetch func(*peerConnection, *fetchRequest) error, cancel func(*fetchRequest), capacity func(*peerConnection) int,
  1202  	idle func() ([]*peerConnection, int), setIdle func(*peerConnection, int), kind string) error {
  1203  
  1204  	// Create a ticker to detect expired retrieval tasks
  1205  	ticker := time.NewTicker(100 * time.Millisecond)
  1206  	defer ticker.Stop()
  1207  
  1208  	update := make(chan struct{}, 1)
  1209  
  1210  	// Prepare the queue and fetch block parts until the block header fetcher's done
  1211  	finished := false
  1212  	for {
  1213  		select {
  1214  		case <-d.cancelCh:
  1215  			return errCanceled
  1216  
  1217  		case packet := <-deliveryCh:
  1218  			// If the peer was previously banned and failed to deliver its pack
  1219  			// in a reasonable time frame, ignore its message.
  1220  			if peer := d.peers.Peer(packet.PeerId()); peer != nil {
  1221  				// Deliver the received chunk of data and check chain validity
  1222  				accepted, err := deliver(packet)
  1223  				if err == errInvalidChain {
  1224  					return err
  1225  				}
  1226  				// Unless a peer delivered something completely else than requested (usually
  1227  				// caused by a timed out request which came through in the end), set it to
  1228  				// idle. If the delivery's stale, the peer should have already been idled.
  1229  				if err != errStaleDelivery {
  1230  					setIdle(peer, accepted)
  1231  				}
  1232  				// Issue a log to the user to see what's going on
  1233  				switch {
  1234  				case err == nil && packet.Items() == 0:
  1235  					peer.log.Trace("Requested data not delivered", "type", kind)
  1236  				case err == nil:
  1237  					peer.log.Trace("Delivered new batch of data", "type", kind, "count", packet.Stats())
  1238  				default:
  1239  					peer.log.Trace("Failed to deliver retrieved data", "type", kind, "err", err)
  1240  				}
  1241  			}
  1242  			// Blocks assembled, try to update the progress
  1243  			select {
  1244  			case update <- struct{}{}:
  1245  			default:
  1246  			}
  1247  
  1248  		case cont := <-wakeCh:
  1249  			// The header fetcher sent a continuation flag, check if it's done
  1250  			if !cont {
  1251  				finished = true
  1252  			}
  1253  			// Headers arrive, try to update the progress
  1254  			select {
  1255  			case update <- struct{}{}:
  1256  			default:
  1257  			}
  1258  
  1259  		case <-ticker.C:
  1260  			// Sanity check update the progress
  1261  			select {
  1262  			case update <- struct{}{}:
  1263  			default:
  1264  			}
  1265  
  1266  		case <-update:
  1267  			// Short circuit if we lost all our peers
  1268  			if d.peers.Len() == 0 {
  1269  				return errNoPeers
  1270  			}
  1271  			// Check for fetch request timeouts and demote the responsible peers
  1272  			for pid, fails := range expire() {
  1273  				if peer := d.peers.Peer(pid); peer != nil {
  1274  					// If a lot of retrieval elements expired, we might have overestimated the remote peer or perhaps
  1275  					// ourselves. Only reset to minimal throughput but don't drop just yet. If even the minimal times
  1276  					// out that sync wise we need to get rid of the peer.
  1277  					//
  1278  					// The reason the minimum threshold is 2 is because the downloader tries to estimate the bandwidth
  1279  					// and latency of a peer separately, which requires pushing the measures capacity a bit and seeing
  1280  					// how response times reacts, to it always requests one more than the minimum (i.e. min 2).
  1281  					if fails > 2 {
  1282  						peer.log.Trace("Data delivery timed out", "type", kind)
  1283  						setIdle(peer, 0)
  1284  					} else {
  1285  						peer.log.Debug("Stalling delivery, dropping", "type", kind)
  1286  
  1287  						if d.dropPeer == nil {
  1288  							// The dropPeer method is nil when `--copydb` is used for a local copy.
  1289  							// Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
  1290  							peer.log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", pid)
  1291  						} else {
  1292  							d.dropPeer(pid)
  1293  
  1294  							// If this peer was the master peer, abort sync immediately
  1295  							d.cancelLock.RLock()
  1296  							master := pid == d.cancelPeer
  1297  							d.cancelLock.RUnlock()
  1298  
  1299  							if master {
  1300  								d.cancel()
  1301  								return errTimeout
  1302  							}
  1303  						}
  1304  					}
  1305  				}
  1306  			}
  1307  			// If there's nothing more to fetch, wait or terminate
  1308  			if pending() == 0 {
  1309  				if !inFlight() && finished {
  1310  					log.Debug("Data fetching completed", "type", kind)
  1311  					return nil
  1312  				}
  1313  				break
  1314  			}
  1315  			// Send a download request to all idle peers, until throttled
  1316  			progressed, throttled, running := false, false, inFlight()
  1317  			idles, total := idle()
  1318  
  1319  			for _, peer := range idles {
  1320  				// Short circuit if throttling activated
  1321  				if throttle() {
  1322  					throttled = true
  1323  					break
  1324  				}
  1325  				// Short circuit if there is no more available task.
  1326  				if pending() == 0 {
  1327  					break
  1328  				}
  1329  				// Reserve a chunk of fetches for a peer. A nil can mean either that
  1330  				// no more headers are available, or that the peer is known not to
  1331  				// have them.
  1332  				request, progress, err := reserve(peer, capacity(peer))
  1333  				if err != nil {
  1334  					return err
  1335  				}
  1336  				if progress {
  1337  					progressed = true
  1338  				}
  1339  				if request == nil {
  1340  					continue
  1341  				}
  1342  				if request.From > 0 {
  1343  					peer.log.Trace("Requesting new batch of data", "type", kind, "from", request.From)
  1344  				} else {
  1345  					peer.log.Trace("Requesting new batch of data", "type", kind, "count", len(request.Headers), "from", request.Headers[0].Number)
  1346  				}
  1347  				// Fetch the chunk and make sure any errors return the hashes to the queue
  1348  				if fetchHook != nil {
  1349  					fetchHook(request.Headers)
  1350  				}
  1351  				if err := fetch(peer, request); err != nil {
  1352  					// Although we could try and make an attempt to fix this, this error really
  1353  					// means that we've double allocated a fetch task to a peer. If that is the
  1354  					// case, the internal state of the downloader and the queue is very wrong so
  1355  					// better hard crash and note the error instead of silently accumulating into
  1356  					// a much bigger issue.
  1357  					panic(fmt.Sprintf("%v: %s fetch assignment failed", peer, kind))
  1358  				}
  1359  				running = true
  1360  			}
  1361  			// Make sure that we have peers available for fetching. If all peers have been tried
  1362  			// and all failed throw an error
  1363  			if !progressed && !throttled && !running && len(idles) == total && pending() > 0 {
  1364  				return errPeersUnavailable
  1365  			}
  1366  		}
  1367  	}
  1368  }
  1369  
  1370  // processHeaders takes batches of retrieved headers from an input channel and
  1371  // keeps processing and scheduling them into the header chain and downloader's
  1372  // queue until the stream ends or a failure occurs.
  1373  func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) error {
  1374  	// Keep a count of uncertain headers to roll back
  1375  	var rollback []*types.Header
  1376  	defer func() {
  1377  		if len(rollback) > 0 {
  1378  			// Flatten the headers and roll them back
  1379  			hashes := make([]common.Hash, len(rollback))
  1380  			for i, header := range rollback {
  1381  				hashes[i] = header.Hash()
  1382  			}
  1383  			lastHeader, lastFastBlock, lastBlock := d.lightchain.CurrentHeader().Number, common.Big0, common.Big0
  1384  			if d.mode != LightSync {
  1385  				lastFastBlock = d.blockchain.CurrentFastBlock().Number()
  1386  				lastBlock = d.blockchain.CurrentBlock().Number()
  1387  			}
  1388  			d.lightchain.Rollback(hashes)
  1389  			curFastBlock, curBlock := common.Big0, common.Big0
  1390  			if d.mode != LightSync {
  1391  				curFastBlock = d.blockchain.CurrentFastBlock().Number()
  1392  				curBlock = d.blockchain.CurrentBlock().Number()
  1393  			}
  1394  			log.Warn("Rolled back headers", "count", len(hashes),
  1395  				"header", fmt.Sprintf("%d->%d", lastHeader, d.lightchain.CurrentHeader().Number),
  1396  				"fast", fmt.Sprintf("%d->%d", lastFastBlock, curFastBlock),
  1397  				"block", fmt.Sprintf("%d->%d", lastBlock, curBlock))
  1398  		}
  1399  	}()
  1400  
  1401  	// Wait for batches of headers to process
  1402  	gotHeaders := false
  1403  
  1404  	for {
  1405  		select {
  1406  		case <-d.cancelCh:
  1407  			return errCanceled
  1408  
  1409  		case headers := <-d.headerProcCh:
  1410  			// Terminate header processing if we synced up
  1411  			if len(headers) == 0 {
  1412  				// Notify everyone that headers are fully processed
  1413  				for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
  1414  					select {
  1415  					case ch <- false:
  1416  					case <-d.cancelCh:
  1417  					}
  1418  				}
  1419  				// If no headers were retrieved at all, the peer violated its TD promise that it had a
  1420  				// better chain compared to ours. The only exception is if its promised blocks were
  1421  				// already imported by other means (e.g. fetcher):
  1422  				//
  1423  				// R <remote peer>, L <local node>: Both at block 10
  1424  				// R: Mine block 11, and propagate it to L
  1425  				// L: Queue block 11 for import
  1426  				// L: Notice that R's head and TD increased compared to ours, start sync
  1427  				// L: Import of block 11 finishes
  1428  				// L: Sync begins, and finds common ancestor at 11
  1429  				// L: Request new headers up from 11 (R's TD was higher, it must have something)
  1430  				// R: Nothing to give
  1431  				if d.mode != LightSync {
  1432  					head := d.blockchain.CurrentBlock()
  1433  					if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 {
  1434  						return errStallingPeer
  1435  					}
  1436  				}
  1437  				// If fast or light syncing, ensure promised headers are indeed delivered. This is
  1438  				// needed to detect scenarios where an attacker feeds a bad pivot and then bails out
  1439  				// of delivering the post-pivot blocks that would flag the invalid content.
  1440  				//
  1441  				// This check cannot be executed "as is" for full imports, since blocks may still be
  1442  				// queued for processing when the header download completes. However, as long as the
  1443  				// peer gave us something useful, we're already happy/progressed (above check).
  1444  				if d.mode == FastSync || d.mode == LightSync {
  1445  					head := d.lightchain.CurrentHeader()
  1446  					if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 {
  1447  						return errStallingPeer
  1448  					}
  1449  				}
  1450  				// Disable any rollback and return
  1451  				rollback = nil
  1452  				return nil
  1453  			}
  1454  			// Otherwise split the chunk of headers into batches and process them
  1455  			gotHeaders = true
  1456  			for len(headers) > 0 {
  1457  				// Terminate if something failed in between processing chunks
  1458  				select {
  1459  				case <-d.cancelCh:
  1460  					return errCanceled
  1461  				default:
  1462  				}
  1463  				// Select the next chunk of headers to import
  1464  				limit := maxHeadersProcess
  1465  				if limit > len(headers) {
  1466  					limit = len(headers)
  1467  				}
  1468  				chunk := headers[:limit]
  1469  				// In case of header only syncing, validate the chunk immediately
  1470  				if d.mode == FastSync || d.mode == LightSync {
  1471  					// Collect the yet unknown headers to mark them as uncertain
  1472  					unknown := make([]*types.Header, 0, len(chunk))
  1473  					for _, header := range chunk {
  1474  						if !d.lightchain.HasHeader(header.Hash(), header.Number.Uint64()) {
  1475  							unknown = append(unknown, header)
  1476  						}
  1477  					}
  1478  					// If we're importing pure headers, verify based on their recentness
  1479  					frequency := fsHeaderCheckFrequency
  1480  					if chunk[len(chunk)-1].Number.Uint64()+uint64(fsHeaderForceVerify) > pivot {
  1481  						frequency = 1
  1482  					}
  1483  					if n, err := d.lightchain.InsertHeaderChain(chunk, frequency); err != nil {
  1484  						// If some headers were inserted, add them too to the rollback list
  1485  						if n > 0 {
  1486  							rollback = append(rollback, chunk[:n]...)
  1487  						}
  1488  						log.Debug("Invalid header encountered", "number", chunk[n].Number, "hash", chunk[n].Hash(), "err", err)
  1489  						return errInvalidChain
  1490  					}
  1491  					// All verifications passed, store newly found uncertain headers
  1492  					rollback = append(rollback, unknown...)
  1493  					if len(rollback) > fsHeaderSafetyNet {
  1494  						rollback = append(rollback[:0], rollback[len(rollback)-fsHeaderSafetyNet:]...)
  1495  					}
  1496  				}
  1497  				// Unless we're doing light chains, schedule the headers for associated content retrieval
  1498  				if d.mode == FullSync || d.mode == FastSync || d.mode == BoundedFullSync {
  1499  					// If we've reached the allowed number of pending headers, stall a bit
  1500  					for d.queue.PendingBlocks() >= maxQueuedHeaders || d.queue.PendingReceipts() >= maxQueuedHeaders {
  1501  						select {
  1502  						case <-d.cancelCh:
  1503  							return errCanceled
  1504  						case <-time.After(time.Second):
  1505  						}
  1506  					}
  1507  					// Otherwise insert the headers for content retrieval
  1508  					inserts := d.queue.Schedule(chunk, origin)
  1509  					if len(inserts) != len(chunk) {
  1510  						log.Debug("Stale headers")
  1511  						return errBadPeer
  1512  					}
  1513  				}
  1514  				headers = headers[limit:]
  1515  				origin += uint64(limit)
  1516  			}
  1517  			// Update the highest block number we know if a higher one is found.
  1518  			d.syncStatsLock.Lock()
  1519  			if d.syncStatsChainHeight < origin {
  1520  				d.syncStatsChainHeight = origin - 1
  1521  			}
  1522  			d.syncStatsLock.Unlock()
  1523  
  1524  			// Signal the content downloaders of the availablility of new tasks
  1525  			for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
  1526  				select {
  1527  				case ch <- true:
  1528  				default:
  1529  				}
  1530  			}
  1531  		}
  1532  	}
  1533  }
  1534  
  1535  // processFullSyncContent takes fetch results from the queue and imports them into the chain.
  1536  func (d *Downloader) processFullSyncContent() error {
  1537  	for {
  1538  		results := d.queue.Results(true)
  1539  		if len(results) == 0 {
  1540  			return nil
  1541  		}
  1542  		if d.chainInsertHook != nil {
  1543  			d.chainInsertHook(results)
  1544  		}
  1545  		if err := d.importBlockResults(results); err != nil {
  1546  			return err
  1547  		}
  1548  	}
  1549  }
  1550  
  1551  func (d *Downloader) importBlockResults(results []*fetchResult) error {
  1552  	// Check for any early termination requests
  1553  	if len(results) == 0 {
  1554  		return nil
  1555  	}
  1556  	select {
  1557  	case <-d.quitCh:
  1558  		return errCancelContentProcessing
  1559  	default:
  1560  	}
  1561  	// Retrieve the a batch of results to import
  1562  	first, last := results[0].Header, results[len(results)-1].Header
  1563  	log.Debug("Inserting downloaded chain", "items", len(results),
  1564  		"firstnum", first.Number, "firsthash", first.Hash(),
  1565  		"lastnum", last.Number, "lasthash", last.Hash(),
  1566  	)
  1567  	blocks := make([]*types.Block, len(results))
  1568  	for i, result := range results {
  1569  		blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1570  	}
  1571  	if index, err := d.blockchain.InsertChain(blocks); err != nil {
  1572  		if index < len(results) {
  1573  			log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
  1574  		} else {
  1575  			// The InsertChain method in blockchain.go will sometimes return an out-of-bounds index,
  1576  			// when it needs to preprocess blocks to import a sidechain.
  1577  			// The importer will put together a new list of blocks to import, which is a superset
  1578  			// of the blocks delivered from the downloader, and the indexing will be off.
  1579  			log.Debug("Downloaded item processing failed on sidechain import", "index", index, "err", err)
  1580  		}
  1581  		return errInvalidChain
  1582  	}
  1583  	return nil
  1584  }
  1585  
  1586  // processFastSyncContent takes fetch results from the queue and writes them to the
  1587  // database. It also controls the synchronisation of state nodes of the pivot block.
  1588  func (d *Downloader) processFastSyncContent(latest *types.Header) error {
  1589  	// Start syncing state of the reported head block. This should get us most of
  1590  	// the state of the pivot block.
  1591  	sync := d.syncState(latest.Root)
  1592  	defer sync.Cancel()
  1593  	closeOnErr := func(s *stateSync) {
  1594  		if err := s.Wait(); err != nil && err != errCancelStateFetch && err != errCanceled {
  1595  			d.queue.Close() // wake up Results
  1596  		}
  1597  	}
  1598  	go closeOnErr(sync)
  1599  	// Figure out the ideal pivot block. Note, that this goalpost may move if the
  1600  	// sync takes long enough for the chain head to move significantly.
  1601  	pivot := uint64(0)
  1602  	if height := latest.Number.Uint64(); height > uint64(fsMinFullBlocks) {
  1603  		pivot = height - uint64(fsMinFullBlocks)
  1604  	}
  1605  	// To cater for moving pivot points, track the pivot block and subsequently
  1606  	// accumulated download results separately.
  1607  	var (
  1608  		oldPivot *fetchResult   // Locked in pivot block, might change eventually
  1609  		oldTail  []*fetchResult // Downloaded content after the pivot
  1610  	)
  1611  	for {
  1612  		// Wait for the next batch of downloaded data to be available, and if the pivot
  1613  		// block became stale, move the goalpost
  1614  		results := d.queue.Results(oldPivot == nil) // Block if we're not monitoring pivot staleness
  1615  		if len(results) == 0 {
  1616  			// If pivot sync is done, stop
  1617  			if oldPivot == nil {
  1618  				return sync.Cancel()
  1619  			}
  1620  			// If sync failed, stop
  1621  			select {
  1622  			case <-d.cancelCh:
  1623  				sync.Cancel()
  1624  				return errCanceled
  1625  			default:
  1626  			}
  1627  		}
  1628  		if d.chainInsertHook != nil {
  1629  			d.chainInsertHook(results)
  1630  		}
  1631  		if oldPivot != nil {
  1632  			results = append(append([]*fetchResult{oldPivot}, oldTail...), results...)
  1633  		}
  1634  		// Split around the pivot block and process the two sides via fast/full sync
  1635  		if atomic.LoadInt32(&d.committed) == 0 {
  1636  			latest = results[len(results)-1].Header
  1637  			if height := latest.Number.Uint64(); height > pivot+2*uint64(fsMinFullBlocks) {
  1638  				log.Warn("Pivot became stale, moving", "old", pivot, "new", height-uint64(fsMinFullBlocks))
  1639  				pivot = height - uint64(fsMinFullBlocks)
  1640  			}
  1641  		}
  1642  		P, beforeP, afterP := splitAroundPivot(pivot, results)
  1643  		if err := d.commitFastSyncData(beforeP, sync); err != nil {
  1644  			return err
  1645  		}
  1646  		if P != nil {
  1647  			// If new pivot block found, cancel old state retrieval and restart
  1648  			if oldPivot != P {
  1649  				sync.Cancel()
  1650  
  1651  				sync = d.syncState(P.Header.Root)
  1652  				defer sync.Cancel()
  1653  				go closeOnErr(sync)
  1654  				oldPivot = P
  1655  			}
  1656  			// Wait for completion, occasionally checking for pivot staleness
  1657  			select {
  1658  			case <-sync.done:
  1659  				if sync.err != nil {
  1660  					return sync.err
  1661  				}
  1662  				if err := d.commitPivotBlock(P); err != nil {
  1663  					return err
  1664  				}
  1665  				oldPivot = nil
  1666  
  1667  			case <-time.After(time.Second):
  1668  				oldTail = afterP
  1669  				continue
  1670  			}
  1671  		}
  1672  		// Fast sync done, pivot commit done, full import
  1673  		if err := d.importBlockResults(afterP); err != nil {
  1674  			return err
  1675  		}
  1676  	}
  1677  }
  1678  
  1679  func splitAroundPivot(pivot uint64, results []*fetchResult) (p *fetchResult, before, after []*fetchResult) {
  1680  	for _, result := range results {
  1681  		num := result.Header.Number.Uint64()
  1682  		switch {
  1683  		case num < pivot:
  1684  			before = append(before, result)
  1685  		case num == pivot:
  1686  			p = result
  1687  		default:
  1688  			after = append(after, result)
  1689  		}
  1690  	}
  1691  	return p, before, after
  1692  }
  1693  
  1694  func (d *Downloader) commitFastSyncData(results []*fetchResult, stateSync *stateSync) error {
  1695  	// Check for any early termination requests
  1696  	if len(results) == 0 {
  1697  		return nil
  1698  	}
  1699  	select {
  1700  	case <-d.quitCh:
  1701  		return errCancelContentProcessing
  1702  	case <-stateSync.done:
  1703  		if err := stateSync.Wait(); err != nil {
  1704  			return err
  1705  		}
  1706  	default:
  1707  	}
  1708  	// Retrieve the a batch of results to import
  1709  	first, last := results[0].Header, results[len(results)-1].Header
  1710  	log.Debug("Inserting fast-sync blocks", "items", len(results),
  1711  		"firstnum", first.Number, "firsthash", first.Hash(),
  1712  		"lastnumn", last.Number, "lasthash", last.Hash(),
  1713  	)
  1714  	blocks := make([]*types.Block, len(results))
  1715  	receipts := make([]types.Receipts, len(results))
  1716  	for i, result := range results {
  1717  		blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1718  		receipts[i] = result.Receipts
  1719  	}
  1720  	if index, err := d.blockchain.InsertReceiptChain(blocks, receipts, d.ancientLimit); err != nil {
  1721  		log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
  1722  		return errInvalidChain
  1723  	}
  1724  	return nil
  1725  }
  1726  
  1727  func (d *Downloader) commitPivotBlock(result *fetchResult) error {
  1728  	block := types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1729  	log.Debug("Committing fast sync pivot as new head", "number", block.Number(), "hash", block.Hash())
  1730  
  1731  	// Commit the pivot block as the new head, will require full sync from here on
  1732  	if _, err := d.blockchain.InsertReceiptChain([]*types.Block{block}, []types.Receipts{result.Receipts}, d.ancientLimit); err != nil {
  1733  		return err
  1734  	}
  1735  	if err := d.blockchain.FastSyncCommitHead(block.Hash()); err != nil {
  1736  		return err
  1737  	}
  1738  	atomic.StoreInt32(&d.committed, 1)
  1739  
  1740  	// If we had a bloom filter for the state sync, deallocate it now. Note, we only
  1741  	// deallocate internally, but keep the empty wrapper. This ensures that if we do
  1742  	// a rollback after committing the pivot and restarting fast sync, we don't end
  1743  	// up using a nil bloom. Empty bloom is fine, it just returns that it does not
  1744  	// have the info we need, so reach down to the database instead.
  1745  	if d.stateBloom != nil {
  1746  		d.stateBloom.Close()
  1747  	}
  1748  	return nil
  1749  }
  1750  
  1751  // DeliverHeaders injects a new batch of block headers received from a remote
  1752  // node into the download schedule.
  1753  func (d *Downloader) DeliverHeaders(id string, headers []*types.Header) (err error) {
  1754  	return d.deliver(id, d.headerCh, &headerPack{id, headers}, headerInMeter, headerDropMeter)
  1755  }
  1756  
  1757  // DeliverBodies injects a new batch of block bodies received from a remote node.
  1758  func (d *Downloader) DeliverBodies(id string, transactions [][]*types.Transaction, uncles [][]*types.Header) (err error) {
  1759  	return d.deliver(id, d.bodyCh, &bodyPack{id, transactions, uncles}, bodyInMeter, bodyDropMeter)
  1760  }
  1761  
  1762  // DeliverReceipts injects a new batch of receipts received from a remote node.
  1763  func (d *Downloader) DeliverReceipts(id string, receipts [][]*types.Receipt) (err error) {
  1764  	return d.deliver(id, d.receiptCh, &receiptPack{id, receipts}, receiptInMeter, receiptDropMeter)
  1765  }
  1766  
  1767  // DeliverNodeData injects a new batch of node state data received from a remote node.
  1768  func (d *Downloader) DeliverNodeData(id string, data [][]byte) (err error) {
  1769  	return d.deliver(id, d.stateCh, &statePack{id, data}, stateInMeter, stateDropMeter)
  1770  }
  1771  
  1772  // deliver injects a new batch of data received from a remote node.
  1773  func (d *Downloader) deliver(id string, destCh chan dataPack, packet dataPack, inMeter, dropMeter metrics.Meter) (err error) {
  1774  	// Update the delivery metrics for both good and failed deliveries
  1775  	inMeter.Mark(int64(packet.Items()))
  1776  	defer func() {
  1777  		if err != nil {
  1778  			dropMeter.Mark(int64(packet.Items()))
  1779  		}
  1780  	}()
  1781  	// Deliver or abort if the sync is canceled while queuing
  1782  	d.cancelLock.RLock()
  1783  	cancel := d.cancelCh
  1784  	d.cancelLock.RUnlock()
  1785  	if cancel == nil {
  1786  		return errNoSyncActive
  1787  	}
  1788  	select {
  1789  	case destCh <- packet:
  1790  		return nil
  1791  	case <-cancel:
  1792  		return errNoSyncActive
  1793  	}
  1794  }
  1795  
  1796  // qosTuner is the quality of service tuning loop that occasionally gathers the
  1797  // peer latency statistics and updates the estimated request round trip time.
  1798  func (d *Downloader) qosTuner() {
  1799  	for {
  1800  		// Retrieve the current median RTT and integrate into the previoust target RTT
  1801  		rtt := time.Duration((1-qosTuningImpact)*float64(atomic.LoadUint64(&d.rttEstimate)) + qosTuningImpact*float64(d.peers.medianRTT()))
  1802  		atomic.StoreUint64(&d.rttEstimate, uint64(rtt))
  1803  
  1804  		// A new RTT cycle passed, increase our confidence in the estimated RTT
  1805  		conf := atomic.LoadUint64(&d.rttConfidence)
  1806  		conf = conf + (1000000-conf)/2
  1807  		atomic.StoreUint64(&d.rttConfidence, conf)
  1808  
  1809  		// Log the new QoS values and sleep until the next RTT
  1810  		log.Debug("Recalculated downloader QoS values", "rtt", rtt, "confidence", float64(conf)/1000000.0, "ttl", d.requestTTL())
  1811  		select {
  1812  		case <-d.quitCh:
  1813  			return
  1814  		case <-time.After(rtt):
  1815  		}
  1816  	}
  1817  }
  1818  
  1819  // qosReduceConfidence is meant to be called when a new peer joins the downloader's
  1820  // peer set, needing to reduce the confidence we have in out QoS estimates.
  1821  func (d *Downloader) qosReduceConfidence() {
  1822  	// If we have a single peer, confidence is always 1
  1823  	peers := uint64(d.peers.Len())
  1824  	if peers == 0 {
  1825  		// Ensure peer connectivity races don't catch us off guard
  1826  		return
  1827  	}
  1828  	if peers == 1 {
  1829  		atomic.StoreUint64(&d.rttConfidence, 1000000)
  1830  		return
  1831  	}
  1832  	// If we have a ton of peers, don't drop confidence)
  1833  	if peers >= uint64(qosConfidenceCap) {
  1834  		return
  1835  	}
  1836  	// Otherwise drop the confidence factor
  1837  	conf := atomic.LoadUint64(&d.rttConfidence) * (peers - 1) / peers
  1838  	if float64(conf)/1000000 < rttMinConfidence {
  1839  		conf = uint64(rttMinConfidence * 1000000)
  1840  	}
  1841  	atomic.StoreUint64(&d.rttConfidence, conf)
  1842  
  1843  	rtt := time.Duration(atomic.LoadUint64(&d.rttEstimate))
  1844  	log.Debug("Relaxed downloader QoS values", "rtt", rtt, "confidence", float64(conf)/1000000.0, "ttl", d.requestTTL())
  1845  }
  1846  
  1847  // requestRTT returns the current target round trip time for a download request
  1848  // to complete in.
  1849  //
  1850  // Note, the returned RTT is .9 of the actually estimated RTT. The reason is that
  1851  // the downloader tries to adapt queries to the RTT, so multiple RTT values can
  1852  // be adapted to, but smaller ones are preferred (stabler download stream).
  1853  func (d *Downloader) requestRTT() time.Duration {
  1854  	return time.Duration(atomic.LoadUint64(&d.rttEstimate)) * 9 / 10
  1855  }
  1856  
  1857  // requestTTL returns the current timeout allowance for a single download request
  1858  // to finish under.
  1859  func (d *Downloader) requestTTL() time.Duration {
  1860  	var (
  1861  		rtt  = time.Duration(atomic.LoadUint64(&d.rttEstimate))
  1862  		conf = float64(atomic.LoadUint64(&d.rttConfidence)) / 1000000.0
  1863  	)
  1864  	ttl := time.Duration(ttlScaling) * time.Duration(float64(rtt)/conf)
  1865  	if ttl > ttlLimit {
  1866  		ttl = ttlLimit
  1867  	}
  1868  	return ttl
  1869  }
  1870  
  1871  // Extra downloader functionality for non-proof-of-work consensus
  1872  
  1873  // Synchronizes with a peer, but only up to the provided Hash
  1874  func (d *Downloader) syncWithPeerUntil(p *peerConnection, hash common.Hash, td *big.Int) (err error) {
  1875  	d.mux.Post(StartEvent{})
  1876  	defer func() {
  1877  		// reset on error
  1878  		if err != nil {
  1879  			d.mux.Post(FailedEvent{err})
  1880  		} else {
  1881  			// Raft syncWithPeerUntil never use the latest field in DoneEvent
  1882  			// therefore post empty DoneEvent only
  1883  			d.mux.Post(DoneEvent{})
  1884  		}
  1885  	}()
  1886  	if p.version < 62 {
  1887  		return errTooOld
  1888  	}
  1889  
  1890  	log.Info("Synchronising with the network", "id", p.id, "version", p.version)
  1891  	defer func(start time.Time) {
  1892  		log.Info("Synchronisation terminated", "duration", time.Since(start))
  1893  	}(time.Now())
  1894  
  1895  	frozen, _ := d.stateDB.Ancients()
  1896  	localHeight := d.blockchain.CurrentBlock().NumberU64()
  1897  
  1898  	// check if recovering state db and only ancient db is present
  1899  	// in this case its possible that local hash is not the latest
  1900  	// as per raft wal. change header to remote header
  1901  	var remoteHeader *types.Header
  1902  	if localHeight == 0 && frozen > 0 {
  1903  		// statedb was removed and is being recovered now
  1904  		// we trust the peer height and sync upto that
  1905  		remoteHeader, err = d.fetchHeight(p)
  1906  	} else {
  1907  		remoteHeader, err = d.fetchHeader(p, hash)
  1908  	}
  1909  	if err != nil {
  1910  		return err
  1911  	}
  1912  
  1913  	remoteHeight := remoteHeader.Number.Uint64()
  1914  
  1915  	d.syncStatsLock.Lock()
  1916  	if d.syncStatsChainHeight <= localHeight || d.syncStatsChainOrigin > localHeight {
  1917  		d.syncStatsChainOrigin = localHeight
  1918  	}
  1919  	d.syncStatsChainHeight = remoteHeight
  1920  	d.syncStatsLock.Unlock()
  1921  
  1922  	d.queue.Prepare(localHeight+1, d.mode)
  1923  	if d.syncInitHook != nil {
  1924  		d.syncInitHook(localHeight, remoteHeight)
  1925  	}
  1926  
  1927  	pivot := uint64(0)
  1928  
  1929  	fetchers := []func() error{
  1930  		func() error { return d.fetchBoundedHeaders(p, localHeight+1, remoteHeight) },
  1931  		func() error { return d.fetchBodies(localHeight + 1) },
  1932  		func() error { return d.fetchReceipts(localHeight + 1) }, // Receipts are only retrieved during fast sync
  1933  		func() error { return d.processHeaders(localHeight+1, pivot, td) },
  1934  		d.processFullSyncContent, //This must be added to clear the buffer of downloaded content as it's being filled
  1935  	}
  1936  	return d.spawnSync(fetchers)
  1937  }
  1938  
  1939  // Fetches a single header from a peer
  1940  func (d *Downloader) fetchHeader(p *peerConnection, hash common.Hash) (*types.Header, error) {
  1941  	log.Info("retrieving remote chain height", "peer", p)
  1942  
  1943  	go p.peer.RequestHeadersByHash(hash, 1, 0, false)
  1944  
  1945  	timeout := time.After(d.requestTTL())
  1946  	for {
  1947  		select {
  1948  		case <-d.cancelCh:
  1949  			return nil, errCancelBlockFetch
  1950  
  1951  		case packet := <-d.headerCh:
  1952  			// Discard anything not from the origin peer
  1953  			if packet.PeerId() != p.id {
  1954  				log.Info("Received headers from incorrect peer", "peer id", packet.PeerId())
  1955  				break
  1956  			}
  1957  			// Make sure the peer actually gave something valid
  1958  			headers := packet.(*headerPack).headers
  1959  			if len(headers) != 1 {
  1960  				log.Info("invalid number of head headers (!= 1)", "peer", p, "len(headers)", len(headers))
  1961  				return nil, errBadPeer
  1962  			}
  1963  			return headers[0], nil
  1964  
  1965  		case <-timeout:
  1966  			log.Info("head header timeout", "peer", p)
  1967  			return nil, errTimeout
  1968  
  1969  		case <-d.bodyCh:
  1970  		case <-d.stateCh:
  1971  		case <-d.receiptCh:
  1972  			// Out of bounds delivery, ignore
  1973  		}
  1974  	}
  1975  }
  1976  
  1977  // Not defined in go's stdlib:
  1978  func minInt(a, b int) int {
  1979  	if a < b {
  1980  		return a
  1981  	}
  1982  	return b
  1983  }
  1984  
  1985  // Fetches headers between `from` and `to`, inclusive.
  1986  // Assumes invariant: from <= to.
  1987  func (d *Downloader) fetchBoundedHeaders(p *peerConnection, from uint64, to uint64) error {
  1988  	log.Info("directing header downloads", "peer", p, "from", from, "to", to)
  1989  	defer log.Info("header download terminated", "peer", p)
  1990  
  1991  	// Create a timeout timer, and the associated header fetcher
  1992  	skeleton := true            // Skeleton assembly phase or finishing up
  1993  	request := time.Now()       // time of the last skeleton fetch request
  1994  	timeout := time.NewTimer(0) // timer to dump a non-responsive active peer
  1995  	<-timeout.C                 // timeout channel should be initially empty
  1996  	defer timeout.Stop()
  1997  
  1998  	getHeaders := func(from uint64) {
  1999  		request = time.Now()
  2000  		timeout.Reset(d.requestTTL())
  2001  
  2002  		skeletonStart := from + uint64(MaxHeaderFetch) - 1
  2003  
  2004  		if skeleton {
  2005  			if skeletonStart > to {
  2006  				skeleton = false
  2007  			}
  2008  		}
  2009  
  2010  		if skeleton {
  2011  			numSkeletonHeaders := minInt(MaxSkeletonSize, (int(to-from)+1)/MaxHeaderFetch)
  2012  			log.Trace("fetching skeleton headers", "peer", p, "num skeleton headers", numSkeletonHeaders, "from", from)
  2013  			go p.peer.RequestHeadersByNumber(skeletonStart, numSkeletonHeaders, MaxHeaderFetch-1, false)
  2014  		} else {
  2015  			// There are not enough headers remaining to warrant a skeleton fetch.
  2016  			// Grab all of the remaining headers.
  2017  
  2018  			numHeaders := int(to-from) + 1
  2019  			log.Trace("fetching full headers", "peer", p, "num headers", numHeaders, "from", from)
  2020  			go p.peer.RequestHeadersByNumber(from, numHeaders, 0, false)
  2021  		}
  2022  	}
  2023  	// Start pulling the header chain skeleton until all is done
  2024  	getHeaders(from)
  2025  
  2026  	for {
  2027  		select {
  2028  		case <-d.cancelCh:
  2029  			return errCancelHeaderFetch
  2030  
  2031  		case packet := <-d.headerCh:
  2032  			// Make sure the active peer is giving us the skeleton headers
  2033  			if packet.PeerId() != p.id {
  2034  				log.Info("Received headers from incorrect peer", "peer id", packet.PeerId())
  2035  				break
  2036  			}
  2037  			headerReqTimer.UpdateSince(request)
  2038  			timeout.Stop()
  2039  
  2040  			headers := packet.(*headerPack).headers
  2041  
  2042  			// If we received a skeleton batch, resolve internals concurrently
  2043  			if skeleton {
  2044  				filled, proced, err := d.fillHeaderSkeleton(from, headers)
  2045  				if err != nil {
  2046  					log.Debug("skeleton chain invalid", "peer", p, "err", err)
  2047  					return errInvalidChain
  2048  				}
  2049  				headers = filled[proced:]
  2050  				from += uint64(proced)
  2051  			}
  2052  			// Insert all the new headers and fetch the next batch
  2053  			if len(headers) > 0 {
  2054  				log.Trace("schedule headers", "peer", p, "num headers", len(headers), "from", from)
  2055  				select {
  2056  				case d.headerProcCh <- headers:
  2057  				case <-d.cancelCh:
  2058  					return errCancelHeaderFetch
  2059  				}
  2060  				from += uint64(len(headers))
  2061  			}
  2062  
  2063  			if from <= to {
  2064  				getHeaders(from)
  2065  			} else {
  2066  				// Notify the content fetchers that no more headers are inbound and return.
  2067  				select {
  2068  				case d.headerProcCh <- nil:
  2069  					return nil
  2070  				case <-d.cancelCh:
  2071  					return errCancelHeaderFetch
  2072  				}
  2073  			}
  2074  
  2075  		case <-timeout.C:
  2076  			// Header retrieval timed out, consider the peer bad and drop
  2077  			log.Info("header request timed out", "peer", p)
  2078  			headerTimeoutMeter.Mark(1)
  2079  			d.dropPeer(p.id)
  2080  
  2081  			// Finish the sync gracefully instead of dumping the gathered data though
  2082  			for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
  2083  				select {
  2084  				case ch <- false:
  2085  				case <-d.cancelCh:
  2086  				}
  2087  			}
  2088  			select {
  2089  			case d.headerProcCh <- nil:
  2090  			case <-d.cancelCh:
  2091  			}
  2092  			return errBadPeer
  2093  		}
  2094  	}
  2095  }