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