github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/fetcher/fetcher.go (about)

     1  // Copyright 2015 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/common"
    26  	"github.com/SmartMeshFoundation/Spectrum/consensus"
    27  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    28  	"github.com/SmartMeshFoundation/Spectrum/log"
    29  	"gopkg.in/karalabe/cookiejar.v2/collections/prque"
    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  // headerFilterTask 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  	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[string]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[string]int          // Per peer block counts to prevent memory exhaustion
   130  	queued map[common.Hash]*inject // Set of already queued blocks (to dedup 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 (eth/61) or header (eth/62) fetch
   144  	completingHook     func([]common.Hash)     // Method to call upon starting a block body fetch (eth/62)
   145  	importedHook       func(*types.Block)      // Method to call upon successful block import (both eth/61 and eth/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[string]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[string]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 string, 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 string, 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 string, 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 string, transactions [][]*types.Transaction, uncles [][]*types.Header, time time.Time) ([][]*types.Transaction, [][]*types.Header) {
   252  	log.Trace("Filtering bodies", "peer", peer, "txs", len(transactions), "uncles", len(uncles))
   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, nil
   261  	}
   262  	// Request the filtering of the body list
   263  	select {
   264  	case filter <- &bodyFilterTask{peer: peer, transactions: transactions, uncles: uncles, time: time}:
   265  	case <-f.quit:
   266  		return nil, nil
   267  	}
   268  	// Retrieve the bodies remaining after filtering
   269  	select {
   270  	case task := <-filter:
   271  		return task.transactions, task.uncles
   272  	case <-f.quit:
   273  		return nil, 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  			if f.queueChangeHook != nil {
   296  				f.queueChangeHook(op.block.Hash(), false)
   297  			}
   298  			// If too high up the chain or phase, continue later
   299  			number := op.block.NumberU64()
   300  			if number > height+1 {
   301  				f.queue.Push(op, -float32(op.block.NumberU64()))
   302  				if f.queueChangeHook != nil {
   303  					f.queueChangeHook(op.block.Hash(), true)
   304  				}
   305  				break
   306  			}
   307  			// Otherwise if fresh and still unknown, try and import
   308  			hash := op.block.Hash()
   309  			if number+maxUncleDist < height || 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 < -maxUncleDist || 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[string][]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[string][]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{}) && header.UncleHash == types.CalcUncleHash([]*types.Header{}) {
   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 < len(task.uncles); 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  						uncleHash := types.CalcUncleHash(task.uncles[i])
   527  
   528  						if txnHash == announce.header.TxHash && uncleHash == announce.header.UncleHash && announce.origin == task.peer {
   529  							// Mark the body matched, reassemble if still unknown
   530  							matched = true
   531  
   532  							if f.getBlock(hash) == nil {
   533  								block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])
   534  								block.ReceivedAt = task.time
   535  
   536  								blocks = append(blocks, block)
   537  							} else {
   538  								f.forgetHash(hash)
   539  							}
   540  						}
   541  					}
   542  				}
   543  				if matched {
   544  					task.transactions = append(task.transactions[:i], task.transactions[i+1:]...)
   545  					task.uncles = append(task.uncles[:i], task.uncles[i+1:]...)
   546  					i--
   547  					continue
   548  				}
   549  			}
   550  
   551  			bodyFilterOutMeter.Mark(int64(len(task.transactions)))
   552  			select {
   553  			case filter <- task:
   554  			case <-f.quit:
   555  				return
   556  			}
   557  			// Schedule the retrieved blocks for ordered import
   558  			for _, block := range blocks {
   559  				if announce := f.completing[block.Hash()]; announce != nil {
   560  					f.enqueue(announce.origin, block)
   561  				}
   562  			}
   563  		}
   564  	}
   565  }
   566  
   567  // rescheduleFetch resets the specified fetch timer to the next announce timeout.
   568  func (f *Fetcher) rescheduleFetch(fetch *time.Timer) {
   569  	// Short circuit if no blocks are announced
   570  	if len(f.announced) == 0 {
   571  		return
   572  	}
   573  	// Otherwise find the earliest expiring announcement
   574  	earliest := time.Now()
   575  	for _, announces := range f.announced {
   576  		if earliest.After(announces[0].time) {
   577  			earliest = announces[0].time
   578  		}
   579  	}
   580  	fetch.Reset(arriveTimeout - time.Since(earliest))
   581  }
   582  
   583  // rescheduleComplete resets the specified completion timer to the next fetch timeout.
   584  func (f *Fetcher) rescheduleComplete(complete *time.Timer) {
   585  	// Short circuit if no headers are fetched
   586  	if len(f.fetched) == 0 {
   587  		return
   588  	}
   589  	// Otherwise find the earliest expiring announcement
   590  	earliest := time.Now()
   591  	for _, announces := range f.fetched {
   592  		if earliest.After(announces[0].time) {
   593  			earliest = announces[0].time
   594  		}
   595  	}
   596  	complete.Reset(gatherSlack - time.Since(earliest))
   597  }
   598  
   599  // enqueue schedules a new future import operation, if the block to be imported
   600  // has not yet been seen.
   601  func (f *Fetcher) enqueue(peer string, block *types.Block) {
   602  	hash := block.Hash()
   603  
   604  	// Ensure the peer isn't DOSing us
   605  	count := f.queues[peer] + 1
   606  	if count > blockLimit {
   607  		log.Debug("Discarded propagated block, exceeded allowance", "peer", peer, "number", block.Number(), "hash", hash, "limit", blockLimit)
   608  		propBroadcastDOSMeter.Mark(1)
   609  		f.forgetHash(hash)
   610  		return
   611  	}
   612  	// Discard any past or too distant blocks
   613  	if dist := int64(block.NumberU64()) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
   614  		log.Debug("Discarded propagated block, too far away", "peer", peer, "number", block.Number(), "hash", hash, "distance", dist)
   615  		propBroadcastDropMeter.Mark(1)
   616  		f.forgetHash(hash)
   617  		return
   618  	}
   619  	// Schedule the block for future importing
   620  	if _, ok := f.queued[hash]; !ok {
   621  		op := &inject{
   622  			origin: peer,
   623  			block:  block,
   624  		}
   625  		f.queues[peer] = count
   626  		f.queued[hash] = op
   627  		f.queue.Push(op, -float32(block.NumberU64()))
   628  		if f.queueChangeHook != nil {
   629  			f.queueChangeHook(op.block.Hash(), true)
   630  		}
   631  		log.Debug("Queued propagated block", "peer", peer, "number", block.Number(), "hash", hash, "queued", f.queue.Size())
   632  	}
   633  }
   634  
   635  // insert spawns a new goroutine to run a block insertion into the chain. If the
   636  // block's number is at the same height as the current import phase, if updates
   637  // the phase states accordingly.
   638  func (f *Fetcher) insert(peer string, block *types.Block) {
   639  	hash := block.Hash()
   640  
   641  	// Run the import on a new thread
   642  	log.Debug("Importing propagated block", "peer", peer, "number", block.Number(), "hash", hash)
   643  	go func() {
   644  		defer func() {
   645  			log.Debug("insert_done", "number", block.Number())
   646  			f.done <- hash
   647  		}()
   648  
   649  		// If the parent's unknown, abort insertion
   650  		parent := f.getBlock(block.ParentHash())
   651  		if parent == nil {
   652  			log.Debug("Unknown parent of propagated block", "peer", peer, "number", block.Number(), "hash", hash, "parent", block.ParentHash())
   653  			return
   654  		}
   655  		// Quickly validate the header and propagate the block if it passes
   656  		switch err := f.verifyHeader(block.Header()); err {
   657  		case nil:
   658  			// All ok, quickly propagate to our peers
   659  			propBroadcastOutTimer.UpdateSince(block.ReceivedAt)
   660  			go f.broadcastBlock(block, true)
   661  
   662  		case consensus.ErrFutureBlock:
   663  			// Weird future block, don't fail, but neither propagate
   664  
   665  		default:
   666  			// Something went very wrong, drop the peer
   667  			log.Debug("Propagated block verification failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   668  			f.dropPeer(peer)
   669  			return
   670  		}
   671  		// Run the actual import and log any issues
   672  		log.Debug("insert_begin", "number", block.Number())
   673  		_, err := f.insertChain(types.Blocks{block})
   674  		log.Debug("insert_end", "number", block.Number(), "err", err)
   675  		if err != nil {
   676  			log.Debug("Propagated block import failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
   677  			return
   678  		}
   679  		// If import succeeded, broadcast the block
   680  		propAnnounceOutTimer.UpdateSince(block.ReceivedAt)
   681  		go f.broadcastBlock(block, false)
   682  
   683  		// Invoke the testing hook if needed
   684  		if f.importedHook != nil {
   685  			f.importedHook(block)
   686  		}
   687  	}()
   688  }
   689  
   690  // forgetHash removes all traces of a block announcement from the fetcher's
   691  // internal state.
   692  func (f *Fetcher) forgetHash(hash common.Hash) {
   693  	// Remove all pending announces and decrement DOS counters
   694  	for _, announce := range f.announced[hash] {
   695  		f.announces[announce.origin]--
   696  		if f.announces[announce.origin] == 0 {
   697  			delete(f.announces, announce.origin)
   698  		}
   699  	}
   700  	delete(f.announced, hash)
   701  	if f.announceChangeHook != nil {
   702  		f.announceChangeHook(hash, false)
   703  	}
   704  	// Remove any pending fetches and decrement the DOS counters
   705  	if announce := f.fetching[hash]; announce != nil {
   706  		f.announces[announce.origin]--
   707  		if f.announces[announce.origin] == 0 {
   708  			delete(f.announces, announce.origin)
   709  		}
   710  		delete(f.fetching, hash)
   711  	}
   712  
   713  	// Remove any pending completion requests and decrement the DOS counters
   714  	for _, announce := range f.fetched[hash] {
   715  		f.announces[announce.origin]--
   716  		if f.announces[announce.origin] == 0 {
   717  			delete(f.announces, announce.origin)
   718  		}
   719  	}
   720  	delete(f.fetched, hash)
   721  
   722  	// Remove any pending completions and decrement the DOS counters
   723  	if announce := f.completing[hash]; announce != nil {
   724  		f.announces[announce.origin]--
   725  		if f.announces[announce.origin] == 0 {
   726  			delete(f.announces, announce.origin)
   727  		}
   728  		delete(f.completing, hash)
   729  	}
   730  }
   731  
   732  // forgetBlock removes all traces of a queued block from the fetcher's internal
   733  // state.
   734  func (f *Fetcher) forgetBlock(hash common.Hash) {
   735  	if insert := f.queued[hash]; insert != nil {
   736  		f.queues[insert.origin]--
   737  		if f.queues[insert.origin] == 0 {
   738  			delete(f.queues, insert.origin)
   739  		}
   740  		delete(f.queued, hash)
   741  	}
   742  }