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