github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/eth/fetcher/fetcher.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // Package fetcher contains the block announcement based synchronisation.
    13  package fetcher
    14  
    15  import (
    16  	"errors"
    17  	"math/rand"
    18  	"time"
    19  
    20  	"github.com/Sberex/go-sberex/common"
    21  	"github.com/Sberex/go-sberex/consensus"
    22  	"github.com/Sberex/go-sberex/core/types"
    23  	"github.com/Sberex/go-sberex/log"
    24  	"gopkg.in/karalabe/cookiejar.v2/collections/prque"
    25  )
    26  
    27  const (
    28  	arriveTimeout = 500 * time.Millisecond // Time allowance before an announced block is explicitly requested
    29  	gatherSlack   = 100 * time.Millisecond // Interval used to collate almost-expired announces with fetches
    30  	fetchTimeout  = 10 * time.Second        // Maximum allotted time to return an explicitly requested block
    31  	maxUncleDist  = 7                      // Maximum allowed backward distance from the chain head
    32  	maxQueueDist  = 32                     // Maximum allowed distance from the chain head to queue
    33  	hashLimit     = 256                    // Maximum number of unique blocks a peer may have announced
    34  	blockLimit    = 64                     // Maximum number of unique blocks a peer may have delivered
    35  )
    36  
    37  var (
    38  	errTerminated = errors.New("terminated")
    39  )
    40  
    41  // blockRetrievalFn is a callback type for retrieving a block from the local chain.
    42  type blockRetrievalFn func(common.Hash) *types.Block
    43  
    44  // headerRequesterFn is a callback type for sending a header retrieval request.
    45  type headerRequesterFn func(common.Hash) error
    46  
    47  // bodyRequesterFn is a callback type for sending a body retrieval request.
    48  type bodyRequesterFn func([]common.Hash) error
    49  
    50  // headerVerifierFn is a callback type to verify a block's header for fast propagation.
    51  type headerVerifierFn func(header *types.Header) error
    52  
    53  // blockBroadcasterFn is a callback type for broadcasting a block to connected peers.
    54  type blockBroadcasterFn func(block *types.Block, propagate bool)
    55  
    56  // chainHeightFn is a callback type to retrieve the current chain height.
    57  type chainHeightFn func() uint64
    58  
    59  // chainInsertFn is a callback type to insert a batch of blocks into the local chain.
    60  type chainInsertFn func(types.Blocks) (int, error)
    61  
    62  // peerDropFn is a callback type for dropping a peer detected as malicious.
    63  type peerDropFn func(id string)
    64  
    65  // announce is the hash notification of the availability of a new block in the
    66  // network.
    67  type announce struct {
    68  	hash   common.Hash   // Hash of the block being announced
    69  	number uint64        // Number of the block being announced (0 = unknown | old protocol)
    70  	header *types.Header // Header of the block partially reassembled (new protocol)
    71  	time   time.Time     // Timestamp of the announcement
    72  
    73  	origin string // Identifier of the peer originating the notification
    74  
    75  	fetchHeader headerRequesterFn // Fetcher function to retrieve the header of an announced block
    76  	fetchBodies bodyRequesterFn   // Fetcher function to retrieve the body of an announced block
    77  }
    78  
    79  // headerFilterTask represents a batch of headers needing fetcher filtering.
    80  type headerFilterTask struct {
    81  	peer    string          // The source peer of block headers
    82  	headers []*types.Header // Collection of headers to filter
    83  	time    time.Time       // Arrival time of the headers
    84  }
    85  
    86  // headerFilterTask represents a batch of block bodies (transactions and uncles)
    87  // needing fetcher filtering.
    88  type bodyFilterTask struct {
    89  	peer         string                 // The source peer of block bodies
    90  	transactions [][]*types.Transaction // Collection of transactions per block bodies
    91  	uncles       [][]*types.Header      // Collection of uncles per block bodies
    92  	time         time.Time              // Arrival time of the blocks' contents
    93  }
    94  
    95  // inject represents a schedules import operation.
    96  type inject struct {
    97  	origin string
    98  	block  *types.Block
    99  }
   100  
   101  // Fetcher is responsible for accumulating block announcements from various peers
   102  // and scheduling them for retrieval.
   103  type Fetcher struct {
   104  	// Various event channels
   105  	notify chan *announce
   106  	inject chan *inject
   107  
   108  	blockFilter  chan chan []*types.Block
   109  	headerFilter chan chan *headerFilterTask
   110  	bodyFilter   chan chan *bodyFilterTask
   111  
   112  	done chan common.Hash
   113  	quit chan struct{}
   114  
   115  	// Announce states
   116  	announces  map[string]int              // Per peer announce counts to prevent memory exhaustion
   117  	announced  map[common.Hash][]*announce // Announced blocks, scheduled for fetching
   118  	fetching   map[common.Hash]*announce   // Announced blocks, currently fetching
   119  	fetched    map[common.Hash][]*announce // Blocks with headers fetched, scheduled for body retrieval
   120  	completing map[common.Hash]*announce   // Blocks with headers, currently body-completing
   121  
   122  	// Block cache
   123  	queue  *prque.Prque            // Queue containing the import operations (block number sorted)
   124  	queues map[string]int          // Per peer block counts to prevent memory exhaustion
   125  	queued map[common.Hash]*inject // Set of already queued blocks (to dedup imports)
   126  
   127  	// Callbacks
   128  	getBlock       blockRetrievalFn   // Retrieves a block from the local chain
   129  	verifyHeader   headerVerifierFn   // Checks if a block's headers have a valid proof of work
   130  	broadcastBlock blockBroadcasterFn // Broadcasts a block to connected peers
   131  	chainHeight    chainHeightFn      // Retrieves the current chain's height
   132  	insertChain    chainInsertFn      // Injects a batch of blocks into the chain
   133  	dropPeer       peerDropFn         // Drops a peer for misbehaving
   134  
   135  	// Testing hooks
   136  	announceChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a hash from the announce list
   137  	queueChangeHook    func(common.Hash, bool) // Method to call upon adding or deleting a block from the import queue
   138  	fetchingHook       func([]common.Hash)     // Method to call upon starting a block (eth/61) or header (eth/62) fetch
   139  	completingHook     func([]common.Hash)     // Method to call upon starting a block body fetch (eth/62)
   140  	importedHook       func(*types.Block)      // Method to call upon successful block import (both eth/61 and eth/62)
   141  }
   142  
   143  // New creates a block fetcher to retrieve blocks based on hash announcements.
   144  func New(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertChain chainInsertFn, dropPeer peerDropFn) *Fetcher {
   145  	return &Fetcher{
   146  		notify:         make(chan *announce),
   147  		inject:         make(chan *inject),
   148  		blockFilter:    make(chan chan []*types.Block),
   149  		headerFilter:   make(chan chan *headerFilterTask),
   150  		bodyFilter:     make(chan chan *bodyFilterTask),
   151  		done:           make(chan common.Hash),
   152  		quit:           make(chan struct{}),
   153  		announces:      make(map[string]int),
   154  		announced:      make(map[common.Hash][]*announce),
   155  		fetching:       make(map[common.Hash]*announce),
   156  		fetched:        make(map[common.Hash][]*announce),
   157  		completing:     make(map[common.Hash]*announce),
   158  		queue:          prque.New(),
   159  		queues:         make(map[string]int),
   160  		queued:         make(map[common.Hash]*inject),
   161  		getBlock:       getBlock,
   162  		verifyHeader:   verifyHeader,
   163  		broadcastBlock: broadcastBlock,
   164  		chainHeight:    chainHeight,
   165  		insertChain:    insertChain,
   166  		dropPeer:       dropPeer,
   167  	}
   168  }
   169  
   170  // Start boots up the announcement based synchroniser, accepting and processing
   171  // hash notifications and block fetches until termination requested.
   172  func (f *Fetcher) Start() {
   173  	go f.loop()
   174  }
   175  
   176  // Stop terminates the announcement based synchroniser, canceling all pending
   177  // operations.
   178  func (f *Fetcher) Stop() {
   179  	close(f.quit)
   180  }
   181  
   182  // Notify announces the fetcher of the potential availability of a new block in
   183  // the network.
   184  func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time,
   185  	headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn) error {
   186  	block := &announce{
   187  		hash:        hash,
   188  		number:      number,
   189  		time:        time,
   190  		origin:      peer,
   191  		fetchHeader: headerFetcher,
   192  		fetchBodies: bodyFetcher,
   193  	}
   194  	select {
   195  	case f.notify <- block:
   196  		return nil
   197  	case <-f.quit:
   198  		return errTerminated
   199  	}
   200  }
   201  
   202  // Enqueue tries to fill gaps the the fetcher's future import queue.
   203  func (f *Fetcher) Enqueue(peer string, block *types.Block) error {
   204  	op := &inject{
   205  		origin: peer,
   206  		block:  block,
   207  	}
   208  	select {
   209  	case f.inject <- op:
   210  		return nil
   211  	case <-f.quit:
   212  		return errTerminated
   213  	}
   214  }
   215  
   216  // FilterHeaders extracts all the headers that were explicitly requested by the fetcher,
   217  // returning those that should be handled differently.
   218  func (f *Fetcher) FilterHeaders(peer string, headers []*types.Header, time time.Time) []*types.Header {
   219  	log.Trace("Filtering headers", "peer", peer, "headers", len(headers))
   220  
   221  	// Send the filter channel to the fetcher
   222  	filter := make(chan *headerFilterTask)
   223  
   224  	select {
   225  	case f.headerFilter <- filter:
   226  	case <-f.quit:
   227  		return nil
   228  	}
   229  	// Request the filtering of the header list
   230  	select {
   231  	case filter <- &headerFilterTask{peer: peer, headers: headers, time: time}:
   232  	case <-f.quit:
   233  		return nil
   234  	}
   235  	// Retrieve the headers remaining after filtering
   236  	select {
   237  	case task := <-filter:
   238  		return task.headers
   239  	case <-f.quit:
   240  		return nil
   241  	}
   242  }
   243  
   244  // FilterBodies extracts all the block bodies that were explicitly requested by
   245  // the fetcher, returning those that should be handled differently.
   246  func (f *Fetcher) FilterBodies(peer string, transactions [][]*types.Transaction, uncles [][]*types.Header, time time.Time) ([][]*types.Transaction, [][]*types.Header) {
   247  	log.Trace("Filtering bodies", "peer", peer, "txs", len(transactions), "uncles", len(uncles))
   248  
   249  	// Send the filter channel to the fetcher
   250  	filter := make(chan *bodyFilterTask)
   251  
   252  	select {
   253  	case f.bodyFilter <- filter:
   254  	case <-f.quit:
   255  		return nil, nil
   256  	}
   257  	// Request the filtering of the body list
   258  	select {
   259  	case filter <- &bodyFilterTask{peer: peer, transactions: transactions, uncles: uncles, time: time}:
   260  	case <-f.quit:
   261  		return nil, nil
   262  	}
   263  	// Retrieve the bodies remaining after filtering
   264  	select {
   265  	case task := <-filter:
   266  		return task.transactions, task.uncles
   267  	case <-f.quit:
   268  		return nil, nil
   269  	}
   270  }
   271  
   272  // Loop is the main fetcher loop, checking and processing various notification
   273  // events.
   274  func (f *Fetcher) loop() {
   275  	// Iterate the block fetching until a quit is requested
   276  	fetchTimer := time.NewTimer(0)
   277  	completeTimer := time.NewTimer(0)
   278  
   279  	for {
   280  		// Clean up any expired block fetches
   281  		for hash, announce := range f.fetching {
   282  			if time.Since(announce.time) > fetchTimeout {
   283  				f.forgetHash(hash)
   284  			}
   285  		}
   286  		// Import any queued blocks that could potentially fit
   287  		height := f.chainHeight()
   288  		for !f.queue.Empty() {
   289  			op := f.queue.PopItem().(*inject)
   290  			if f.queueChangeHook != nil {
   291  				f.queueChangeHook(op.block.Hash(), false)
   292  			}
   293  			// If too high up the chain or phase, continue later
   294  			number := op.block.NumberU64()
   295  			if number > height+1 {
   296  				f.queue.Push(op, -float32(op.block.NumberU64()))
   297  				if f.queueChangeHook != nil {
   298  					f.queueChangeHook(op.block.Hash(), true)
   299  				}
   300  				break
   301  			}
   302  			// Otherwise if fresh and still unknown, try and import
   303  			hash := op.block.Hash()
   304  			if number+maxUncleDist < height || f.getBlock(hash) != nil {
   305  				f.forgetBlock(hash)
   306  				continue
   307  			}
   308  			f.insert(op.origin, op.block)
   309  		}
   310  		// Wait for an outside event to occur
   311  		select {
   312  		case <-f.quit:
   313  			// Fetcher terminating, abort all operations
   314  			return
   315  
   316  		case notification := <-f.notify:
   317  			// A block was announced, make sure the peer isn't DOSing us
   318  			propAnnounceInMeter.Mark(1)
   319  
   320  			count := f.announces[notification.origin] + 1
   321  			if count > hashLimit {
   322  				log.Debug("Peer exceeded outstanding announces", "peer", notification.origin, "limit", hashLimit)
   323  				propAnnounceDOSMeter.Mark(1)
   324  				break
   325  			}
   326  			// If we have a valid block number, check that it's potentially useful
   327  			if notification.number > 0 {
   328  				if dist := int64(notification.number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
   329  					log.Debug("Peer discarded announcement", "peer", notification.origin, "number", notification.number, "hash", notification.hash, "distance", dist)
   330  					propAnnounceDropMeter.Mark(1)
   331  					break
   332  				}
   333  			}
   334  			// All is well, schedule the announce if block's not yet downloading
   335  			if _, ok := f.fetching[notification.hash]; ok {
   336  				break
   337  			}
   338  			if _, ok := f.completing[notification.hash]; ok {
   339  				break
   340  			}
   341  			f.announces[notification.origin] = count
   342  			f.announced[notification.hash] = append(f.announced[notification.hash], notification)
   343  			if f.announceChangeHook != nil && len(f.announced[notification.hash]) == 1 {
   344  				f.announceChangeHook(notification.hash, true)
   345  			}
   346  			if len(f.announced) == 1 {
   347  				f.rescheduleFetch(fetchTimer)
   348  			}
   349  
   350  		case op := <-f.inject:
   351  			// A direct block insertion was requested, try and fill any pending gaps
   352  			propBroadcastInMeter.Mark(1)
   353  			f.enqueue(op.origin, op.block)
   354  
   355  		case hash := <-f.done:
   356  			// A pending import finished, remove all traces of the notification
   357  			f.forgetHash(hash)
   358  			f.forgetBlock(hash)
   359  
   360  		case <-fetchTimer.C:
   361  			// At least one block's timer ran out, check for needing retrieval
   362  			request := make(map[string][]common.Hash)
   363  
   364  			for hash, announces := range f.announced {
   365  				if time.Since(announces[0].time) > arriveTimeout-gatherSlack {
   366  					// Pick a random peer to retrieve from, reset all others
   367  					announce := announces[rand.Intn(len(announces))]
   368  					f.forgetHash(hash)
   369  
   370  					// If the block still didn't arrive, queue for fetching
   371  					if f.getBlock(hash) == nil {
   372  						request[announce.origin] = append(request[announce.origin], hash)
   373  						f.fetching[hash] = announce
   374  					}
   375  				}
   376  			}
   377  			// Send out all block header requests
   378  			for peer, hashes := range request {
   379  				log.Trace("Fetching scheduled headers", "peer", peer, "list", hashes)
   380  
   381  				// Create a closure of the fetch and schedule in on a new thread
   382  				fetchHeader, hashes := f.fetching[hashes[0]].fetchHeader, hashes
   383  				go func() {
   384  					if f.fetchingHook != nil {
   385  						f.fetchingHook(hashes)
   386  					}
   387  					for _, hash := range hashes {
   388  						headerFetchMeter.Mark(1)
   389  						fetchHeader(hash) // Suboptimal, but protocol doesn't allow batch header retrievals
   390  					}
   391  				}()
   392  			}
   393  			// Schedule the next fetch if blocks are still pending
   394  			f.rescheduleFetch(fetchTimer)
   395  
   396  		case <-completeTimer.C:
   397  			// At least one header's timer ran out, retrieve everything
   398  			request := make(map[string][]common.Hash)
   399  
   400  			for hash, announces := range f.fetched {
   401  				// Pick a random peer to retrieve from, reset all others
   402  				announce := announces[rand.Intn(len(announces))]
   403  				f.forgetHash(hash)
   404  
   405  				// If the block still didn't arrive, queue for completion
   406  				if f.getBlock(hash) == nil {
   407  					request[announce.origin] = append(request[announce.origin], hash)
   408  					f.completing[hash] = announce
   409  				}
   410  			}
   411  			// Send out all block body requests
   412  			for peer, hashes := range request {
   413  				log.Trace("Fetching scheduled bodies", "peer", peer, "list", hashes)
   414  
   415  				// Create a closure of the fetch and schedule in on a new thread
   416  				if f.completingHook != nil {
   417  					f.completingHook(hashes)
   418  				}
   419  				bodyFetchMeter.Mark(int64(len(hashes)))
   420  				go f.completing[hashes[0]].fetchBodies(hashes)
   421  			}
   422  			// Schedule the next fetch if blocks are still pending
   423  			f.rescheduleComplete(completeTimer)
   424  
   425  		case filter := <-f.headerFilter:
   426  			// Headers arrived from a remote peer. Extract those that were explicitly
   427  			// requested by the fetcher, and return everything else so it's delivered
   428  			// to other parts of the system.
   429  			var task *headerFilterTask
   430  			select {
   431  			case task = <-filter:
   432  			case <-f.quit:
   433  				return
   434  			}
   435  			headerFilterInMeter.Mark(int64(len(task.headers)))
   436  
   437  			// Split the batch of headers into unknown ones (to return to the caller),
   438  			// known incomplete ones (requiring body retrievals) and completed blocks.
   439  			unknown, incomplete, complete := []*types.Header{}, []*announce{}, []*types.Block{}
   440  			for _, header := range task.headers {
   441  				hash := header.Hash()
   442  
   443  				// Filter fetcher-requested headers from other synchronisation algorithms
   444  				if announce := f.fetching[hash]; announce != nil && announce.origin == task.peer && f.fetched[hash] == nil && f.completing[hash] == nil && f.queued[hash] == nil {
   445  					// If the delivered header does not match the promised number, drop the announcer
   446  					if header.Number.Uint64() != announce.number {
   447  						log.Trace("Invalid block number fetched", "peer", announce.origin, "hash", header.Hash(), "announced", announce.number, "provided", header.Number)
   448  						f.dropPeer(announce.origin)
   449  						f.forgetHash(hash)
   450  						continue
   451  					}
   452  					// Only keep if not imported by other means
   453  					if f.getBlock(hash) == nil {
   454  						announce.header = header
   455  						announce.time = task.time
   456  
   457  						// If the block is empty (header only), short circuit into the final import queue
   458  						if header.TxHash == types.DeriveSha(types.Transactions{}) && header.UncleHash == types.CalcUncleHash([]*types.Header{}) {
   459  							log.Trace("Block empty, skipping body retrieval", "peer", announce.origin, "number", header.Number, "hash", header.Hash())
   460  
   461  							block := types.NewBlockWithHeader(header)
   462  							block.ReceivedAt = task.time
   463  
   464  							complete = append(complete, block)
   465  							f.completing[hash] = announce
   466  							continue
   467  						}
   468  						// Otherwise add to the list of blocks needing completion
   469  						incomplete = append(incomplete, announce)
   470  					} else {
   471  						log.Trace("Block already imported, discarding header", "peer", announce.origin, "number", header.Number, "hash", header.Hash())
   472  						f.forgetHash(hash)
   473  					}
   474  				} else {
   475  					// Fetcher doesn't know about it, add to the return list
   476  					unknown = append(unknown, header)
   477  				}
   478  			}
   479  			headerFilterOutMeter.Mark(int64(len(unknown)))
   480  			select {
   481  			case filter <- &headerFilterTask{headers: unknown, time: task.time}:
   482  			case <-f.quit:
   483  				return
   484  			}
   485  			// Schedule the retrieved headers for body completion
   486  			for _, announce := range incomplete {
   487  				hash := announce.header.Hash()
   488  				if _, ok := f.completing[hash]; ok {
   489  					continue
   490  				}
   491  				f.fetched[hash] = append(f.fetched[hash], announce)
   492  				if len(f.fetched) == 1 {
   493  					f.rescheduleComplete(completeTimer)
   494  				}
   495  			}
   496  			// Schedule the header-only blocks for import
   497  			for _, block := range complete {
   498  				if announce := f.completing[block.Hash()]; announce != nil {
   499  					f.enqueue(announce.origin, block)
   500  				}
   501  			}
   502  
   503  		case filter := <-f.bodyFilter:
   504  			// Block bodies arrived, extract any explicitly requested blocks, return the rest
   505  			var task *bodyFilterTask
   506  			select {
   507  			case task = <-filter:
   508  			case <-f.quit:
   509  				return
   510  			}
   511  			bodyFilterInMeter.Mark(int64(len(task.transactions)))
   512  
   513  			blocks := []*types.Block{}
   514  			for i := 0; i < len(task.transactions) && i < len(task.uncles); i++ {
   515  				// Match up a body to any possible completion request
   516  				matched := false
   517  
   518  				for hash, announce := range f.completing {
   519  					if f.queued[hash] == nil {
   520  						txnHash := types.DeriveSha(types.Transactions(task.transactions[i]))
   521  						uncleHash := types.CalcUncleHash(task.uncles[i])
   522  
   523  						if txnHash == announce.header.TxHash && uncleHash == announce.header.UncleHash && announce.origin == task.peer {
   524  							// Mark the body matched, reassemble if still unknown
   525  							matched = true
   526  
   527  							if f.getBlock(hash) == nil {
   528  								block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])
   529  								block.ReceivedAt = task.time
   530  
   531  								blocks = append(blocks, block)
   532  							} else {
   533  								f.forgetHash(hash)
   534  							}
   535  						}
   536  					}
   537  				}
   538  				if matched {
   539  					task.transactions = append(task.transactions[:i], task.transactions[i+1:]...)
   540  					task.uncles = append(task.uncles[:i], task.uncles[i+1:]...)
   541  					i--
   542  					continue
   543  				}
   544  			}
   545  
   546  			bodyFilterOutMeter.Mark(int64(len(task.transactions)))
   547  			select {
   548  			case filter <- task:
   549  			case <-f.quit:
   550  				return
   551  			}
   552  			// Schedule the retrieved blocks for ordered import
   553  			for _, block := range blocks {
   554  				if announce := f.completing[block.Hash()]; announce != nil {
   555  					f.enqueue(announce.origin, block)
   556  				}
   557  			}
   558  		}
   559  	}
   560  }
   561  
   562  // rescheduleFetch resets the specified fetch timer to the next announce timeout.
   563  func (f *Fetcher) rescheduleFetch(fetch *time.Timer) {
   564  	// Short circuit if no blocks are announced
   565  	if len(f.announced) == 0 {
   566  		return
   567  	}
   568  	// Otherwise find the earliest expiring announcement
   569  	earliest := time.Now()
   570  	for _, announces := range f.announced {
   571  		if earliest.After(announces[0].time) {
   572  			earliest = announces[0].time
   573  		}
   574  	}
   575  	fetch.Reset(arriveTimeout - time.Since(earliest))
   576  }
   577  
   578  // rescheduleComplete resets the specified completion timer to the next fetch timeout.
   579  func (f *Fetcher) rescheduleComplete(complete *time.Timer) {
   580  	// Short circuit if no headers are fetched
   581  	if len(f.fetched) == 0 {
   582  		return
   583  	}
   584  	// Otherwise find the earliest expiring announcement
   585  	earliest := time.Now()
   586  	for _, announces := range f.fetched {
   587  		if earliest.After(announces[0].time) {
   588  			earliest = announces[0].time
   589  		}
   590  	}
   591  	complete.Reset(gatherSlack - time.Since(earliest))
   592  }
   593  
   594  // enqueue schedules a new future import operation, if the block to be imported
   595  // has not yet been seen.
   596  func (f *Fetcher) enqueue(peer string, block *types.Block) {
   597  	hash := block.Hash()
   598  
   599  	// Ensure the peer isn't DOSing us
   600  	count := f.queues[peer] + 1
   601  	if count > blockLimit {
   602  		log.Debug("Discarded propagated block, exceeded allowance", "peer", peer, "number", block.Number(), "hash", hash, "limit", blockLimit)
   603  		propBroadcastDOSMeter.Mark(1)
   604  		f.forgetHash(hash)
   605  		return
   606  	}
   607  	// Discard any past or too distant blocks
   608  	if dist := int64(block.NumberU64()) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
   609  		log.Debug("Discarded propagated block, too far away", "peer", peer, "number", block.Number(), "hash", hash, "distance", dist)
   610  		propBroadcastDropMeter.Mark(1)
   611  		f.forgetHash(hash)
   612  		return
   613  	}
   614  	// Schedule the block for future importing
   615  	if _, ok := f.queued[hash]; !ok {
   616  		op := &inject{
   617  			origin: peer,
   618  			block:  block,
   619  		}
   620  		f.queues[peer] = count
   621  		f.queued[hash] = op
   622  		f.queue.Push(op, -float32(block.NumberU64()))
   623  		if f.queueChangeHook != nil {
   624  			f.queueChangeHook(op.block.Hash(), true)
   625  		}
   626  		log.Debug("Queued propagated block", "peer", peer, "number", block.Number(), "hash", hash, "queued", f.queue.Size())
   627  	}
   628  }
   629  
   630  // insert spawns a new goroutine to run a block insertion into the chain. If the
   631  // block's number is at the same height as the current import phase, if updates
   632  // the phase states accordingly.
   633  func (f *Fetcher) insert(peer string, block *types.Block) {
   634  	hash := block.Hash()
   635  
   636  	// Run the import on a new thread
   637  	log.Debug("Importing propagated block", "peer", peer, "number", block.Number(), "hash", hash)
   638  	go func() {
   639  		defer func() { f.done <- hash }()
   640  
   641  		// If the parent's unknown, abort insertion
   642  		parent := f.getBlock(block.ParentHash())
   643  		if parent == nil {
   644  			log.Debug("Unknown parent of propagated block", "peer", peer, "number", block.Number(), "hash", hash, "parent", block.ParentHash())
   645  			return
   646  		}
   647  		// Quickly validate the header and propagate the block if it passes
   648  		switch err := f.verifyHeader(block.Header()); err {
   649  		case nil:
   650  			// All ok, quickly propagate to our peers
   651  			propBroadcastOutTimer.UpdateSince(block.ReceivedAt)
   652  			go f.broadcastBlock(block, true)
   653  
   654  		case consensus.ErrFutureBlock:
   655  			// Weird future block, don't fail, but neither propagate
   656  
   657  		default:
   658  			// Something went very wrong, drop the peer
   659  			log.Debug("Propagated block verification failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   660  			f.dropPeer(peer)
   661  			return
   662  		}
   663  		// Run the actual import and log any issues
   664  		if _, err := f.insertChain(types.Blocks{block}); err != nil {
   665  			log.Debug("Propagated block import failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   666  			return
   667  		}
   668  		// If import succeeded, broadcast the block
   669  		propAnnounceOutTimer.UpdateSince(block.ReceivedAt)
   670  		go f.broadcastBlock(block, false)
   671  
   672  		// Invoke the testing hook if needed
   673  		if f.importedHook != nil {
   674  			f.importedHook(block)
   675  		}
   676  	}()
   677  }
   678  
   679  // forgetHash removes all traces of a block announcement from the fetcher's
   680  // internal state.
   681  func (f *Fetcher) forgetHash(hash common.Hash) {
   682  	// Remove all pending announces and decrement DOS counters
   683  	for _, announce := range f.announced[hash] {
   684  		f.announces[announce.origin]--
   685  		if f.announces[announce.origin] == 0 {
   686  			delete(f.announces, announce.origin)
   687  		}
   688  	}
   689  	delete(f.announced, hash)
   690  	if f.announceChangeHook != nil {
   691  		f.announceChangeHook(hash, false)
   692  	}
   693  	// Remove any pending fetches and decrement the DOS counters
   694  	if announce := f.fetching[hash]; announce != nil {
   695  		f.announces[announce.origin]--
   696  		if f.announces[announce.origin] == 0 {
   697  			delete(f.announces, announce.origin)
   698  		}
   699  		delete(f.fetching, hash)
   700  	}
   701  
   702  	// Remove any pending completion requests and decrement the DOS counters
   703  	for _, announce := range f.fetched[hash] {
   704  		f.announces[announce.origin]--
   705  		if f.announces[announce.origin] == 0 {
   706  			delete(f.announces, announce.origin)
   707  		}
   708  	}
   709  	delete(f.fetched, hash)
   710  
   711  	// Remove any pending completions and decrement the DOS counters
   712  	if announce := f.completing[hash]; announce != nil {
   713  		f.announces[announce.origin]--
   714  		if f.announces[announce.origin] == 0 {
   715  			delete(f.announces, announce.origin)
   716  		}
   717  		delete(f.completing, hash)
   718  	}
   719  }
   720  
   721  // forgetBlock removes all traces of a queued block from the fetcher's internal
   722  // state.
   723  func (f *Fetcher) forgetBlock(hash common.Hash) {
   724  	if insert := f.queued[hash]; insert != nil {
   725  		f.queues[insert.origin]--
   726  		if f.queues[insert.origin] == 0 {
   727  			delete(f.queues, insert.origin)
   728  		}
   729  		delete(f.queued, hash)
   730  	}
   731  }