github.com/phillinzzz/newBsc@v1.1.6/eth/fetcher/block_fetcher.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 fetcher contains the announcement based header, blocks or transaction synchronisation.
    18  package fetcher
    19  
    20  import (
    21  	"errors"
    22  	"math/rand"
    23  	"time"
    24  
    25  	"github.com/phillinzzz/newBsc/common"
    26  	"github.com/phillinzzz/newBsc/common/gopool"
    27  	"github.com/phillinzzz/newBsc/common/prque"
    28  	"github.com/phillinzzz/newBsc/consensus"
    29  	"github.com/phillinzzz/newBsc/core/types"
    30  	"github.com/phillinzzz/newBsc/log"
    31  	"github.com/phillinzzz/newBsc/metrics"
    32  	"github.com/phillinzzz/newBsc/trie"
    33  )
    34  
    35  const (
    36  	lightTimeout        = time.Millisecond       // Time allowance before an announced header is explicitly requested
    37  	arriveTimeout       = 500 * time.Millisecond // Time allowance before an announced block/transaction is explicitly requested
    38  	gatherSlack         = 100 * time.Millisecond // Interval used to collate almost-expired announces with fetches
    39  	fetchTimeout        = 5 * time.Second        // Maximum allotted time to return an explicitly requested block/transaction
    40  	reQueueBlockTimeout = 500 * time.Millisecond // Time allowance before blocks are requeued for import
    41  
    42  )
    43  
    44  const (
    45  	maxUncleDist = 11  // Maximum allowed backward distance from the chain head
    46  	maxQueueDist = 32  // Maximum allowed distance from the chain head to queue
    47  	hashLimit    = 256 // Maximum number of unique blocks or headers a peer may have announced
    48  	blockLimit   = 64  // Maximum number of unique blocks a peer may have delivered
    49  )
    50  
    51  var (
    52  	blockAnnounceInMeter   = metrics.NewRegisteredMeter("eth/fetcher/block/announces/in", nil)
    53  	blockAnnounceOutTimer  = metrics.NewRegisteredTimer("eth/fetcher/block/announces/out", nil)
    54  	blockAnnounceDropMeter = metrics.NewRegisteredMeter("eth/fetcher/block/announces/drop", nil)
    55  	blockAnnounceDOSMeter  = metrics.NewRegisteredMeter("eth/fetcher/block/announces/dos", nil)
    56  
    57  	blockBroadcastInMeter   = metrics.NewRegisteredMeter("eth/fetcher/block/broadcasts/in", nil)
    58  	blockBroadcastOutTimer  = metrics.NewRegisteredTimer("eth/fetcher/block/broadcasts/out", nil)
    59  	blockBroadcastDropMeter = metrics.NewRegisteredMeter("eth/fetcher/block/broadcasts/drop", nil)
    60  	blockBroadcastDOSMeter  = metrics.NewRegisteredMeter("eth/fetcher/block/broadcasts/dos", nil)
    61  
    62  	headerFetchMeter = metrics.NewRegisteredMeter("eth/fetcher/block/headers", nil)
    63  	bodyFetchMeter   = metrics.NewRegisteredMeter("eth/fetcher/block/bodies", nil)
    64  
    65  	headerFilterInMeter  = metrics.NewRegisteredMeter("eth/fetcher/block/filter/headers/in", nil)
    66  	headerFilterOutMeter = metrics.NewRegisteredMeter("eth/fetcher/block/filter/headers/out", nil)
    67  	bodyFilterInMeter    = metrics.NewRegisteredMeter("eth/fetcher/block/filter/bodies/in", nil)
    68  	bodyFilterOutMeter   = metrics.NewRegisteredMeter("eth/fetcher/block/filter/bodies/out", nil)
    69  )
    70  
    71  var errTerminated = errors.New("terminated")
    72  
    73  // HeaderRetrievalFn is a callback type for retrieving a header from the local chain.
    74  type HeaderRetrievalFn func(common.Hash) *types.Header
    75  
    76  // blockRetrievalFn is a callback type for retrieving a block from the local chain.
    77  type blockRetrievalFn func(common.Hash) *types.Block
    78  
    79  // headerRequesterFn is a callback type for sending a header retrieval request.
    80  type headerRequesterFn func(common.Hash) error
    81  
    82  // bodyRequesterFn is a callback type for sending a body retrieval request.
    83  type bodyRequesterFn func([]common.Hash) error
    84  
    85  // DiffRequesterFn is a callback type for sending a diff layer retrieval request.
    86  type DiffRequesterFn func([]common.Hash) error
    87  
    88  // headerVerifierFn is a callback type to verify a block's header for fast propagation.
    89  type headerVerifierFn func(header *types.Header) error
    90  
    91  // blockBroadcasterFn is a callback type for broadcasting a block to connected peers.
    92  type blockBroadcasterFn func(block *types.Block, propagate bool)
    93  
    94  // chainHeightFn is a callback type to retrieve the current chain height.
    95  type chainHeightFn func() uint64
    96  
    97  // headersInsertFn is a callback type to insert a batch of headers into the local chain.
    98  type headersInsertFn func(headers []*types.Header) (int, error)
    99  
   100  // chainInsertFn is a callback type to insert a batch of blocks into the local chain.
   101  type chainInsertFn func(types.Blocks) (int, error)
   102  
   103  // peerDropFn is a callback type for dropping a peer detected as malicious.
   104  type peerDropFn func(id string)
   105  
   106  // blockAnnounce is the hash notification of the availability of a new block in the
   107  // network.
   108  type blockAnnounce struct {
   109  	hash   common.Hash   // Hash of the block being announced
   110  	number uint64        // Number of the block being announced (0 = unknown | old protocol)
   111  	header *types.Header // Header of the block partially reassembled (new protocol)
   112  	time   time.Time     // Timestamp of the announcement
   113  
   114  	origin string // Identifier of the peer originating the notification
   115  
   116  	fetchHeader headerRequesterFn // Fetcher function to retrieve the header of an announced block
   117  	fetchBodies bodyRequesterFn   // Fetcher function to retrieve the body of an announced block
   118  	fetchDiffs  DiffRequesterFn   // Fetcher function to retrieve the diff layer of an announced block
   119  
   120  }
   121  
   122  // headerFilterTask represents a batch of headers needing fetcher filtering.
   123  type headerFilterTask struct {
   124  	peer    string          // The source peer of block headers
   125  	headers []*types.Header // Collection of headers to filter
   126  	time    time.Time       // Arrival time of the headers
   127  }
   128  
   129  // bodyFilterTask represents a batch of block bodies (transactions and uncles)
   130  // needing fetcher filtering.
   131  type bodyFilterTask struct {
   132  	peer         string                 // The source peer of block bodies
   133  	transactions [][]*types.Transaction // Collection of transactions per block bodies
   134  	uncles       [][]*types.Header      // Collection of uncles per block bodies
   135  	time         time.Time              // Arrival time of the blocks' contents
   136  }
   137  
   138  // blockOrHeaderInject represents a schedules import operation.
   139  type blockOrHeaderInject struct {
   140  	origin string
   141  
   142  	header *types.Header // Used for light mode fetcher which only cares about header.
   143  	block  *types.Block  // Used for normal mode fetcher which imports full block.
   144  }
   145  
   146  // number returns the block number of the injected object.
   147  func (inject *blockOrHeaderInject) number() uint64 {
   148  	if inject.header != nil {
   149  		return inject.header.Number.Uint64()
   150  	}
   151  	return inject.block.NumberU64()
   152  }
   153  
   154  // number returns the block hash of the injected object.
   155  func (inject *blockOrHeaderInject) hash() common.Hash {
   156  	if inject.header != nil {
   157  		return inject.header.Hash()
   158  	}
   159  	return inject.block.Hash()
   160  }
   161  
   162  // BlockFetcher is responsible for accumulating block announcements from various peers
   163  // and scheduling them for retrieval.
   164  type BlockFetcher struct {
   165  	light bool // The indicator whether it's a light fetcher or normal one.
   166  
   167  	// Various event channels
   168  	notify chan *blockAnnounce
   169  	inject chan *blockOrHeaderInject
   170  
   171  	headerFilter chan chan *headerFilterTask
   172  	bodyFilter   chan chan *bodyFilterTask
   173  
   174  	done chan common.Hash
   175  	quit chan struct{}
   176  
   177  	requeue chan *blockOrHeaderInject
   178  
   179  	// Announce states
   180  	announces  map[string]int                   // Per peer blockAnnounce counts to prevent memory exhaustion
   181  	announced  map[common.Hash][]*blockAnnounce // Announced blocks, scheduled for fetching
   182  	fetching   map[common.Hash]*blockAnnounce   // Announced blocks, currently fetching
   183  	fetched    map[common.Hash][]*blockAnnounce // Blocks with headers fetched, scheduled for body retrieval
   184  	completing map[common.Hash]*blockAnnounce   // Blocks with headers, currently body-completing
   185  
   186  	// Block cache
   187  	queue  *prque.Prque                         // Queue containing the import operations (block number sorted)
   188  	queues map[string]int                       // Per peer block counts to prevent memory exhaustion
   189  	queued map[common.Hash]*blockOrHeaderInject // Set of already queued blocks (to dedup imports)
   190  
   191  	// Callbacks
   192  	getHeader      HeaderRetrievalFn  // Retrieves a header from the local chain
   193  	getBlock       blockRetrievalFn   // Retrieves a block from the local chain
   194  	verifyHeader   headerVerifierFn   // Checks if a block's headers have a valid proof of work
   195  	broadcastBlock blockBroadcasterFn // Broadcasts a block to connected peers
   196  	chainHeight    chainHeightFn      // Retrieves the current chain's height
   197  	insertHeaders  headersInsertFn    // Injects a batch of headers into the chain
   198  	insertChain    chainInsertFn      // Injects a batch of blocks into the chain
   199  	dropPeer       peerDropFn         // Drops a peer for misbehaving
   200  
   201  	// Testing hooks
   202  	announceChangeHook func(common.Hash, bool)           // Method to call upon adding or deleting a hash from the blockAnnounce list
   203  	queueChangeHook    func(common.Hash, bool)           // Method to call upon adding or deleting a block from the import queue
   204  	fetchingHook       func([]common.Hash)               // Method to call upon starting a block (eth/61) or header (eth/62) fetch
   205  	completingHook     func([]common.Hash)               // Method to call upon starting a block body fetch (eth/62)
   206  	importedHook       func(*types.Header, *types.Block) // Method to call upon successful header or block import (both eth/61 and eth/62)
   207  }
   208  
   209  // NewBlockFetcher creates a block fetcher to retrieve blocks based on hash announcements.
   210  func NewBlockFetcher(light bool, getHeader HeaderRetrievalFn, getBlock blockRetrievalFn, verifyHeader headerVerifierFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertHeaders headersInsertFn, insertChain chainInsertFn, dropPeer peerDropFn) *BlockFetcher {
   211  	return &BlockFetcher{
   212  		light:          light,
   213  		notify:         make(chan *blockAnnounce),
   214  		inject:         make(chan *blockOrHeaderInject),
   215  		headerFilter:   make(chan chan *headerFilterTask),
   216  		bodyFilter:     make(chan chan *bodyFilterTask),
   217  		done:           make(chan common.Hash),
   218  		quit:           make(chan struct{}),
   219  		requeue:        make(chan *blockOrHeaderInject),
   220  		announces:      make(map[string]int),
   221  		announced:      make(map[common.Hash][]*blockAnnounce),
   222  		fetching:       make(map[common.Hash]*blockAnnounce),
   223  		fetched:        make(map[common.Hash][]*blockAnnounce),
   224  		completing:     make(map[common.Hash]*blockAnnounce),
   225  		queue:          prque.New(nil),
   226  		queues:         make(map[string]int),
   227  		queued:         make(map[common.Hash]*blockOrHeaderInject),
   228  		getHeader:      getHeader,
   229  		getBlock:       getBlock,
   230  		verifyHeader:   verifyHeader,
   231  		broadcastBlock: broadcastBlock,
   232  		chainHeight:    chainHeight,
   233  		insertHeaders:  insertHeaders,
   234  		insertChain:    insertChain,
   235  		dropPeer:       dropPeer,
   236  	}
   237  }
   238  
   239  // Start boots up the announcement based synchroniser, accepting and processing
   240  // hash notifications and block fetches until termination requested.
   241  func (f *BlockFetcher) Start() {
   242  	go f.loop()
   243  }
   244  
   245  // Stop terminates the announcement based synchroniser, canceling all pending
   246  // operations.
   247  func (f *BlockFetcher) Stop() {
   248  	close(f.quit)
   249  }
   250  
   251  // Notify announces the fetcher of the potential availability of a new block in
   252  // the network.
   253  func (f *BlockFetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time,
   254  	headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn, diffFetcher DiffRequesterFn) error {
   255  	block := &blockAnnounce{
   256  		hash:        hash,
   257  		number:      number,
   258  		time:        time,
   259  		origin:      peer,
   260  		fetchHeader: headerFetcher,
   261  		fetchBodies: bodyFetcher,
   262  		fetchDiffs:  diffFetcher,
   263  	}
   264  	select {
   265  	case f.notify <- block:
   266  		return nil
   267  	case <-f.quit:
   268  		return errTerminated
   269  	}
   270  }
   271  
   272  // Enqueue tries to fill gaps the fetcher's future import queue.
   273  func (f *BlockFetcher) Enqueue(peer string, block *types.Block) error {
   274  	op := &blockOrHeaderInject{
   275  		origin: peer,
   276  		block:  block,
   277  	}
   278  	select {
   279  	case f.inject <- op:
   280  		return nil
   281  	case <-f.quit:
   282  		return errTerminated
   283  	}
   284  }
   285  
   286  // FilterHeaders extracts all the headers that were explicitly requested by the fetcher,
   287  // returning those that should be handled differently.
   288  func (f *BlockFetcher) FilterHeaders(peer string, headers []*types.Header, time time.Time) []*types.Header {
   289  	log.Trace("Filtering headers", "peer", peer, "headers", len(headers))
   290  
   291  	// Send the filter channel to the fetcher
   292  	filter := make(chan *headerFilterTask)
   293  
   294  	select {
   295  	case f.headerFilter <- filter:
   296  	case <-f.quit:
   297  		return nil
   298  	}
   299  	// Request the filtering of the header list
   300  	select {
   301  	case filter <- &headerFilterTask{peer: peer, headers: headers, time: time}:
   302  	case <-f.quit:
   303  		return nil
   304  	}
   305  	// Retrieve the headers remaining after filtering
   306  	select {
   307  	case task := <-filter:
   308  		return task.headers
   309  	case <-f.quit:
   310  		return nil
   311  	}
   312  }
   313  
   314  // FilterBodies extracts all the block bodies that were explicitly requested by
   315  // the fetcher, returning those that should be handled differently.
   316  func (f *BlockFetcher) FilterBodies(peer string, transactions [][]*types.Transaction, uncles [][]*types.Header, time time.Time) ([][]*types.Transaction, [][]*types.Header) {
   317  	log.Trace("Filtering bodies", "peer", peer, "txs", len(transactions), "uncles", len(uncles))
   318  
   319  	// Send the filter channel to the fetcher
   320  	filter := make(chan *bodyFilterTask)
   321  
   322  	select {
   323  	case f.bodyFilter <- filter:
   324  	case <-f.quit:
   325  		return nil, nil
   326  	}
   327  	// Request the filtering of the body list
   328  	select {
   329  	case filter <- &bodyFilterTask{peer: peer, transactions: transactions, uncles: uncles, time: time}:
   330  	case <-f.quit:
   331  		return nil, nil
   332  	}
   333  	// Retrieve the bodies remaining after filtering
   334  	select {
   335  	case task := <-filter:
   336  		return task.transactions, task.uncles
   337  	case <-f.quit:
   338  		return nil, nil
   339  	}
   340  }
   341  
   342  // Loop is the main fetcher loop, checking and processing various notification
   343  // events.
   344  func (f *BlockFetcher) loop() {
   345  	// Iterate the block fetching until a quit is requested
   346  	var (
   347  		fetchTimer    = time.NewTimer(0)
   348  		completeTimer = time.NewTimer(0)
   349  	)
   350  	<-fetchTimer.C // clear out the channel
   351  	<-completeTimer.C
   352  	defer fetchTimer.Stop()
   353  	defer completeTimer.Stop()
   354  
   355  	for {
   356  		// Clean up any expired block fetches
   357  		for hash, announce := range f.fetching {
   358  			if time.Since(announce.time) > fetchTimeout {
   359  				f.forgetHash(hash)
   360  			}
   361  		}
   362  		// Import any queued blocks that could potentially fit
   363  		height := f.chainHeight()
   364  		for !f.queue.Empty() {
   365  			op := f.queue.PopItem().(*blockOrHeaderInject)
   366  			hash := op.hash()
   367  			if f.queueChangeHook != nil {
   368  				f.queueChangeHook(hash, false)
   369  			}
   370  			// If too high up the chain or phase, continue later
   371  			number := op.number()
   372  			if number > height+1 {
   373  				f.queue.Push(op, -int64(number))
   374  				if f.queueChangeHook != nil {
   375  					f.queueChangeHook(hash, true)
   376  				}
   377  				break
   378  			}
   379  			// Otherwise if fresh and still unknown, try and import
   380  			if (number+maxUncleDist < height) || (f.light && f.getHeader(hash) != nil) || (!f.light && f.getBlock(hash) != nil) {
   381  				f.forgetBlock(hash)
   382  				continue
   383  			}
   384  			if f.light {
   385  				f.importHeaders(op)
   386  			} else {
   387  				f.importBlocks(op)
   388  			}
   389  		}
   390  		// Wait for an outside event to occur
   391  		select {
   392  		case <-f.quit:
   393  			// BlockFetcher terminating, abort all operations
   394  			return
   395  
   396  		case notification := <-f.notify:
   397  			// A block was announced, make sure the peer isn't DOSing us
   398  			blockAnnounceInMeter.Mark(1)
   399  
   400  			count := f.announces[notification.origin] + 1
   401  			if count > hashLimit {
   402  				log.Debug("Peer exceeded outstanding announces", "peer", notification.origin, "limit", hashLimit)
   403  				blockAnnounceDOSMeter.Mark(1)
   404  				break
   405  			}
   406  			// If we have a valid block number, check that it's potentially useful
   407  			if notification.number > 0 {
   408  				if dist := int64(notification.number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
   409  					log.Debug("Peer discarded announcement", "peer", notification.origin, "number", notification.number, "hash", notification.hash, "distance", dist)
   410  					blockAnnounceDropMeter.Mark(1)
   411  					break
   412  				}
   413  			}
   414  			// All is well, schedule the announce if block's not yet downloading
   415  			if _, ok := f.fetching[notification.hash]; ok {
   416  				break
   417  			}
   418  			if _, ok := f.completing[notification.hash]; ok {
   419  				break
   420  			}
   421  			f.announces[notification.origin] = count
   422  			f.announced[notification.hash] = append(f.announced[notification.hash], notification)
   423  			if f.announceChangeHook != nil && len(f.announced[notification.hash]) == 1 {
   424  				f.announceChangeHook(notification.hash, true)
   425  			}
   426  			if len(f.announced) == 1 {
   427  				f.rescheduleFetch(fetchTimer)
   428  			}
   429  
   430  		case op := <-f.requeue:
   431  			// Re-queue blocks that have not been written due to fork block competition
   432  			number := int64(0)
   433  			hash := ""
   434  			if op.header != nil {
   435  				number = op.header.Number.Int64()
   436  				hash = op.header.Hash().String()
   437  			} else if op.block != nil {
   438  				number = op.block.Number().Int64()
   439  				hash = op.block.Hash().String()
   440  			}
   441  
   442  			log.Info("Re-queue blocks", "number", number, "hash", hash)
   443  			f.enqueue(op.origin, op.header, op.block)
   444  
   445  		case op := <-f.inject:
   446  			// A direct block insertion was requested, try and fill any pending gaps
   447  			blockBroadcastInMeter.Mark(1)
   448  
   449  			// Now only direct block injection is allowed, drop the header injection
   450  			// here silently if we receive.
   451  			if f.light {
   452  				continue
   453  			}
   454  			f.enqueue(op.origin, nil, op.block)
   455  
   456  		case hash := <-f.done:
   457  			// A pending import finished, remove all traces of the notification
   458  			f.forgetHash(hash)
   459  			f.forgetBlock(hash)
   460  
   461  		case <-fetchTimer.C:
   462  			// At least one block's timer ran out, check for needing retrieval
   463  			request := make(map[string][]common.Hash)
   464  
   465  			for hash, announces := range f.announced {
   466  				// In current LES protocol(les2/les3), only header announce is
   467  				// available, no need to wait too much time for header broadcast.
   468  				timeout := arriveTimeout - gatherSlack
   469  				if f.light {
   470  					timeout = 0
   471  				}
   472  				if time.Since(announces[0].time) > timeout {
   473  					// Pick a random peer to retrieve from, reset all others
   474  					announce := announces[rand.Intn(len(announces))]
   475  					f.forgetHash(hash)
   476  
   477  					// If the block still didn't arrive, queue for fetching
   478  					if (f.light && f.getHeader(hash) == nil) || (!f.light && f.getBlock(hash) == nil) {
   479  						request[announce.origin] = append(request[announce.origin], hash)
   480  						f.fetching[hash] = announce
   481  					}
   482  				}
   483  			}
   484  			// Send out all block header requests
   485  			for peer, hashes := range request {
   486  				log.Trace("Fetching scheduled headers", "peer", peer, "list", hashes)
   487  
   488  				// Create a closure of the fetch and schedule in on a new thread
   489  				fetchHeader, hashes := f.fetching[hashes[0]].fetchHeader, hashes
   490  				fetchDiff := f.fetching[hashes[0]].fetchDiffs
   491  
   492  				gopool.Submit(func() {
   493  					if f.fetchingHook != nil {
   494  						f.fetchingHook(hashes)
   495  					}
   496  					if fetchDiff != nil {
   497  						fetchDiff(hashes)
   498  					}
   499  					for _, hash := range hashes {
   500  						headerFetchMeter.Mark(1)
   501  						fetchHeader(hash) // Suboptimal, but protocol doesn't allow batch header retrievals
   502  					}
   503  				})
   504  			}
   505  			// Schedule the next fetch if blocks are still pending
   506  			f.rescheduleFetch(fetchTimer)
   507  
   508  		case <-completeTimer.C:
   509  			// At least one header's timer ran out, retrieve everything
   510  			request := make(map[string][]common.Hash)
   511  
   512  			for hash, announces := range f.fetched {
   513  				// Pick a random peer to retrieve from, reset all others
   514  				announce := announces[rand.Intn(len(announces))]
   515  				f.forgetHash(hash)
   516  
   517  				// If the block still didn't arrive, queue for completion
   518  				if f.getBlock(hash) == nil {
   519  					request[announce.origin] = append(request[announce.origin], hash)
   520  					f.completing[hash] = announce
   521  				}
   522  			}
   523  			// Send out all block body requests
   524  			for peer, hashes := range request {
   525  				log.Trace("Fetching scheduled bodies", "peer", peer, "list", hashes)
   526  
   527  				// Create a closure of the fetch and schedule in on a new thread
   528  				if f.completingHook != nil {
   529  					f.completingHook(hashes)
   530  				}
   531  				bodyFetchMeter.Mark(int64(len(hashes)))
   532  				go f.completing[hashes[0]].fetchBodies(hashes)
   533  			}
   534  			// Schedule the next fetch if blocks are still pending
   535  			f.rescheduleComplete(completeTimer)
   536  
   537  		case filter := <-f.headerFilter:
   538  			// Headers arrived from a remote peer. Extract those that were explicitly
   539  			// requested by the fetcher, and return everything else so it's delivered
   540  			// to other parts of the system.
   541  			var task *headerFilterTask
   542  			select {
   543  			case task = <-filter:
   544  			case <-f.quit:
   545  				return
   546  			}
   547  			headerFilterInMeter.Mark(int64(len(task.headers)))
   548  
   549  			// Split the batch of headers into unknown ones (to return to the caller),
   550  			// known incomplete ones (requiring body retrievals) and completed blocks.
   551  			unknown, incomplete, complete, lightHeaders := []*types.Header{}, []*blockAnnounce{}, []*types.Block{}, []*blockAnnounce{}
   552  			for _, header := range task.headers {
   553  				hash := header.Hash()
   554  
   555  				// Filter fetcher-requested headers from other synchronisation algorithms
   556  				if announce := f.fetching[hash]; announce != nil && announce.origin == task.peer && f.fetched[hash] == nil && f.completing[hash] == nil && f.queued[hash] == nil {
   557  					// If the delivered header does not match the promised number, drop the announcer
   558  					if header.Number.Uint64() != announce.number {
   559  						log.Trace("Invalid block number fetched", "peer", announce.origin, "hash", header.Hash(), "announced", announce.number, "provided", header.Number)
   560  						f.dropPeer(announce.origin)
   561  						f.forgetHash(hash)
   562  						continue
   563  					}
   564  					// Collect all headers only if we are running in light
   565  					// mode and the headers are not imported by other means.
   566  					if f.light {
   567  						if f.getHeader(hash) == nil {
   568  							announce.header = header
   569  							lightHeaders = append(lightHeaders, announce)
   570  						}
   571  						f.forgetHash(hash)
   572  						continue
   573  					}
   574  					// Only keep if not imported by other means
   575  					if f.getBlock(hash) == nil {
   576  						announce.header = header
   577  						announce.time = task.time
   578  
   579  						// If the block is empty (header only), short circuit into the final import queue
   580  						if header.TxHash == types.EmptyRootHash && header.UncleHash == types.EmptyUncleHash {
   581  							log.Trace("Block empty, skipping body retrieval", "peer", announce.origin, "number", header.Number, "hash", header.Hash())
   582  
   583  							block := types.NewBlockWithHeader(header)
   584  							block.ReceivedAt = task.time
   585  
   586  							complete = append(complete, block)
   587  							f.completing[hash] = announce
   588  							continue
   589  						}
   590  						// Otherwise add to the list of blocks needing completion
   591  						incomplete = append(incomplete, announce)
   592  					} else {
   593  						log.Trace("Block already imported, discarding header", "peer", announce.origin, "number", header.Number, "hash", header.Hash())
   594  						f.forgetHash(hash)
   595  					}
   596  				} else {
   597  					// BlockFetcher doesn't know about it, add to the return list
   598  					unknown = append(unknown, header)
   599  				}
   600  			}
   601  			headerFilterOutMeter.Mark(int64(len(unknown)))
   602  			select {
   603  			case filter <- &headerFilterTask{headers: unknown, time: task.time}:
   604  			case <-f.quit:
   605  				return
   606  			}
   607  			// Schedule the retrieved headers for body completion
   608  			for _, announce := range incomplete {
   609  				hash := announce.header.Hash()
   610  				if _, ok := f.completing[hash]; ok {
   611  					continue
   612  				}
   613  				f.fetched[hash] = append(f.fetched[hash], announce)
   614  				if len(f.fetched) == 1 {
   615  					f.rescheduleComplete(completeTimer)
   616  				}
   617  			}
   618  			// Schedule the header for light fetcher import
   619  			for _, announce := range lightHeaders {
   620  				f.enqueue(announce.origin, announce.header, nil)
   621  			}
   622  			// Schedule the header-only blocks for import
   623  			for _, block := range complete {
   624  				if announce := f.completing[block.Hash()]; announce != nil {
   625  					f.enqueue(announce.origin, nil, block)
   626  				}
   627  			}
   628  
   629  		case filter := <-f.bodyFilter:
   630  			// Block bodies arrived, extract any explicitly requested blocks, return the rest
   631  			var task *bodyFilterTask
   632  			select {
   633  			case task = <-filter:
   634  			case <-f.quit:
   635  				return
   636  			}
   637  			bodyFilterInMeter.Mark(int64(len(task.transactions)))
   638  			blocks := []*types.Block{}
   639  			// abort early if there's nothing explicitly requested
   640  			if len(f.completing) > 0 {
   641  				for i := 0; i < len(task.transactions) && i < len(task.uncles); i++ {
   642  					// Match up a body to any possible completion request
   643  					var (
   644  						matched   = false
   645  						uncleHash common.Hash // calculated lazily and reused
   646  						txnHash   common.Hash // calculated lazily and reused
   647  					)
   648  					for hash, announce := range f.completing {
   649  						if f.queued[hash] != nil || announce.origin != task.peer {
   650  							continue
   651  						}
   652  						if uncleHash == (common.Hash{}) {
   653  							uncleHash = types.CalcUncleHash(task.uncles[i])
   654  						}
   655  						if uncleHash != announce.header.UncleHash {
   656  							continue
   657  						}
   658  						if txnHash == (common.Hash{}) {
   659  							txnHash = types.DeriveSha(types.Transactions(task.transactions[i]), trie.NewStackTrie(nil))
   660  						}
   661  						if txnHash != announce.header.TxHash {
   662  							continue
   663  						}
   664  						// Mark the body matched, reassemble if still unknown
   665  						matched = true
   666  						if f.getBlock(hash) == nil {
   667  							block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])
   668  							block.ReceivedAt = task.time
   669  							blocks = append(blocks, block)
   670  						} else {
   671  							f.forgetHash(hash)
   672  						}
   673  
   674  					}
   675  					if matched {
   676  						task.transactions = append(task.transactions[:i], task.transactions[i+1:]...)
   677  						task.uncles = append(task.uncles[:i], task.uncles[i+1:]...)
   678  						i--
   679  						continue
   680  					}
   681  				}
   682  			}
   683  			bodyFilterOutMeter.Mark(int64(len(task.transactions)))
   684  			select {
   685  			case filter <- task:
   686  			case <-f.quit:
   687  				return
   688  			}
   689  			// Schedule the retrieved blocks for ordered import
   690  			for _, block := range blocks {
   691  				if announce := f.completing[block.Hash()]; announce != nil {
   692  					f.enqueue(announce.origin, nil, block)
   693  				}
   694  			}
   695  		}
   696  	}
   697  }
   698  
   699  // rescheduleFetch resets the specified fetch timer to the next blockAnnounce timeout.
   700  func (f *BlockFetcher) rescheduleFetch(fetch *time.Timer) {
   701  	// Short circuit if no blocks are announced
   702  	if len(f.announced) == 0 {
   703  		return
   704  	}
   705  	// Schedule announcement retrieval quickly for light mode
   706  	// since server won't send any headers to client.
   707  	if f.light {
   708  		fetch.Reset(lightTimeout)
   709  		return
   710  	}
   711  	// Otherwise find the earliest expiring announcement
   712  	earliest := time.Now()
   713  	for _, announces := range f.announced {
   714  		if earliest.After(announces[0].time) {
   715  			earliest = announces[0].time
   716  		}
   717  	}
   718  	fetch.Reset(arriveTimeout - time.Since(earliest))
   719  }
   720  
   721  // rescheduleComplete resets the specified completion timer to the next fetch timeout.
   722  func (f *BlockFetcher) rescheduleComplete(complete *time.Timer) {
   723  	// Short circuit if no headers are fetched
   724  	if len(f.fetched) == 0 {
   725  		return
   726  	}
   727  	// Otherwise find the earliest expiring announcement
   728  	earliest := time.Now()
   729  	for _, announces := range f.fetched {
   730  		if earliest.After(announces[0].time) {
   731  			earliest = announces[0].time
   732  		}
   733  	}
   734  	complete.Reset(gatherSlack - time.Since(earliest))
   735  }
   736  
   737  // enqueue schedules a new header or block import operation, if the component
   738  // to be imported has not yet been seen.
   739  func (f *BlockFetcher) enqueue(peer string, header *types.Header, block *types.Block) {
   740  	var (
   741  		hash   common.Hash
   742  		number uint64
   743  	)
   744  	if header != nil {
   745  		hash, number = header.Hash(), header.Number.Uint64()
   746  	} else {
   747  		hash, number = block.Hash(), block.NumberU64()
   748  	}
   749  	// Ensure the peer isn't DOSing us
   750  	count := f.queues[peer] + 1
   751  	if count > blockLimit {
   752  		log.Debug("Discarded delivered header or block, exceeded allowance", "peer", peer, "number", number, "hash", hash, "limit", blockLimit)
   753  		blockBroadcastDOSMeter.Mark(1)
   754  		f.forgetHash(hash)
   755  		return
   756  	}
   757  	// Discard any past or too distant blocks
   758  	if dist := int64(number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
   759  		log.Debug("Discarded delivered header or block, too far away", "peer", peer, "number", number, "hash", hash, "distance", dist)
   760  		blockBroadcastDropMeter.Mark(1)
   761  		f.forgetHash(hash)
   762  		return
   763  	}
   764  	// Schedule the block for future importing
   765  	if _, ok := f.queued[hash]; !ok {
   766  		op := &blockOrHeaderInject{origin: peer}
   767  		if header != nil {
   768  			op.header = header
   769  		} else {
   770  			op.block = block
   771  		}
   772  		f.queues[peer] = count
   773  		f.queued[hash] = op
   774  		f.queue.Push(op, -int64(number))
   775  		if f.queueChangeHook != nil {
   776  			f.queueChangeHook(hash, true)
   777  		}
   778  		log.Debug("Queued delivered header or block", "peer", peer, "number", number, "hash", hash, "queued", f.queue.Size())
   779  	}
   780  }
   781  
   782  // importHeaders spawns a new goroutine to run a header insertion into the chain.
   783  // If the header's number is at the same height as the current import phase, it
   784  // updates the phase states accordingly.
   785  func (f *BlockFetcher) importHeaders(op *blockOrHeaderInject) {
   786  	peer := op.origin
   787  	header := op.header
   788  	hash := header.Hash()
   789  	log.Debug("Importing propagated header", "peer", peer, "number", header.Number, "hash", hash)
   790  
   791  	go func() {
   792  		defer func() { f.done <- hash }()
   793  		// If the parent's unknown, abort insertion
   794  		parent := f.getHeader(header.ParentHash)
   795  		if parent == nil {
   796  			log.Debug("Unknown parent of propagated header", "peer", peer, "number", header.Number, "hash", hash, "parent", header.ParentHash)
   797  			time.Sleep(reQueueBlockTimeout)
   798  			f.requeue <- op
   799  			return
   800  		}
   801  		// Validate the header and if something went wrong, drop the peer
   802  		if err := f.verifyHeader(header); err != nil && err != consensus.ErrFutureBlock {
   803  			log.Debug("Propagated header verification failed", "peer", peer, "number", header.Number, "hash", hash, "err", err)
   804  			f.dropPeer(peer)
   805  			return
   806  		}
   807  		// Run the actual import and log any issues
   808  		if _, err := f.insertHeaders([]*types.Header{header}); err != nil {
   809  			log.Debug("Propagated header import failed", "peer", peer, "number", header.Number, "hash", hash, "err", err)
   810  			return
   811  		}
   812  		// Invoke the testing hook if needed
   813  		if f.importedHook != nil {
   814  			f.importedHook(header, nil)
   815  		}
   816  	}()
   817  }
   818  
   819  // importBlocks spawns a new goroutine to run a block insertion into the chain. If the
   820  // block's number is at the same height as the current import phase, it updates
   821  // the phase states accordingly.
   822  func (f *BlockFetcher) importBlocks(op *blockOrHeaderInject) {
   823  	peer := op.origin
   824  	block := op.block
   825  	hash := block.Hash()
   826  
   827  	// Run the import on a new thread
   828  	log.Debug("Importing propagated block", "peer", peer, "number", block.Number(), "hash", hash)
   829  	go func() {
   830  		defer func() { f.done <- hash }()
   831  
   832  		// If the parent's unknown, abort insertion
   833  		parent := f.getBlock(block.ParentHash())
   834  		if parent == nil {
   835  			log.Debug("Unknown parent of propagated block", "peer", peer, "number", block.Number(), "hash", hash, "parent", block.ParentHash())
   836  			time.Sleep(reQueueBlockTimeout)
   837  			f.requeue <- op
   838  			return
   839  		}
   840  		// Quickly validate the header and propagate the block if it passes
   841  		switch err := f.verifyHeader(block.Header()); err {
   842  		case nil:
   843  			// All ok, quickly propagate to our peers
   844  			blockBroadcastOutTimer.UpdateSince(block.ReceivedAt)
   845  			go f.broadcastBlock(block, true)
   846  
   847  		case consensus.ErrFutureBlock:
   848  			log.Error("Received future block", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   849  			f.dropPeer(peer)
   850  
   851  		default:
   852  			// Something went very wrong, drop the peer
   853  			log.Error("Propagated block verification failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   854  			f.dropPeer(peer)
   855  			return
   856  		}
   857  		// Run the actual import and log any issues
   858  		if _, err := f.insertChain(types.Blocks{block}); err != nil {
   859  			log.Debug("Propagated block import failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   860  			return
   861  		}
   862  		// If import succeeded, broadcast the block
   863  		blockAnnounceOutTimer.UpdateSince(block.ReceivedAt)
   864  		go f.broadcastBlock(block, false)
   865  
   866  		// Invoke the testing hook if needed
   867  		if f.importedHook != nil {
   868  			f.importedHook(nil, block)
   869  		}
   870  	}()
   871  }
   872  
   873  // forgetHash removes all traces of a block announcement from the fetcher's
   874  // internal state.
   875  func (f *BlockFetcher) forgetHash(hash common.Hash) {
   876  	// Remove all pending announces and decrement DOS counters
   877  	for _, announce := range f.announced[hash] {
   878  		f.announces[announce.origin]--
   879  		if f.announces[announce.origin] <= 0 {
   880  			delete(f.announces, announce.origin)
   881  		}
   882  	}
   883  	delete(f.announced, hash)
   884  	if f.announceChangeHook != nil {
   885  		f.announceChangeHook(hash, false)
   886  	}
   887  	// Remove any pending fetches and decrement the DOS counters
   888  	if announce := f.fetching[hash]; announce != nil {
   889  		f.announces[announce.origin]--
   890  		if f.announces[announce.origin] <= 0 {
   891  			delete(f.announces, announce.origin)
   892  		}
   893  		delete(f.fetching, hash)
   894  	}
   895  
   896  	// Remove any pending completion requests and decrement the DOS counters
   897  	for _, announce := range f.fetched[hash] {
   898  		f.announces[announce.origin]--
   899  		if f.announces[announce.origin] <= 0 {
   900  			delete(f.announces, announce.origin)
   901  		}
   902  	}
   903  	delete(f.fetched, hash)
   904  
   905  	// Remove any pending completions and decrement the DOS counters
   906  	if announce := f.completing[hash]; announce != nil {
   907  		f.announces[announce.origin]--
   908  		if f.announces[announce.origin] <= 0 {
   909  			delete(f.announces, announce.origin)
   910  		}
   911  		delete(f.completing, hash)
   912  	}
   913  }
   914  
   915  // forgetBlock removes all traces of a queued block from the fetcher's internal
   916  // state.
   917  func (f *BlockFetcher) forgetBlock(hash common.Hash) {
   918  	if insert := f.queued[hash]; insert != nil {
   919  		f.queues[insert.origin]--
   920  		if f.queues[insert.origin] == 0 {
   921  			delete(f.queues, insert.origin)
   922  		}
   923  		delete(f.queued, hash)
   924  	}
   925  }