github.com/klaytn/klaytn@v1.12.1/datasync/fetcher/fetcher.go (about)

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