github.com/clem109/go-ethereum@v1.8.3-0.20180316121352-fe6cf00f480a/eth/downloader/queue.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  // Contains the block download scheduler to collect download tasks and schedule
    18  // them in an ordered, and throttled way.
    19  
    20  package downloader
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"sync"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/log"
    31  	"github.com/ethereum/go-ethereum/metrics"
    32  	"gopkg.in/karalabe/cookiejar.v2/collections/prque"
    33  )
    34  
    35  var (
    36  	blockCacheItems      = 8192             // Maximum number of blocks to cache before throttling the download
    37  	blockCacheMemory     = 64 * 1024 * 1024 // Maximum amount of memory to use for block caching
    38  	blockCacheSizeWeight = 0.1              // Multiplier to approximate the average block size based on past ones
    39  )
    40  
    41  var (
    42  	errNoFetchesPending = errors.New("no fetches pending")
    43  	errStaleDelivery    = errors.New("stale delivery")
    44  )
    45  
    46  // fetchRequest is a currently running data retrieval operation.
    47  type fetchRequest struct {
    48  	Peer    *peerConnection // Peer to which the request was sent
    49  	From    uint64          // [eth/62] Requested chain element index (used for skeleton fills only)
    50  	Headers []*types.Header // [eth/62] Requested headers, sorted by request order
    51  	Time    time.Time       // Time when the request was made
    52  }
    53  
    54  // fetchResult is a struct collecting partial results from data fetchers until
    55  // all outstanding pieces complete and the result as a whole can be processed.
    56  type fetchResult struct {
    57  	Pending int         // Number of data fetches still pending
    58  	Hash    common.Hash // Hash of the header to prevent recalculating
    59  
    60  	Header       *types.Header
    61  	Uncles       []*types.Header
    62  	Transactions types.Transactions
    63  	Receipts     types.Receipts
    64  }
    65  
    66  // queue represents hashes that are either need fetching or are being fetched
    67  type queue struct {
    68  	mode SyncMode // Synchronisation mode to decide on the block parts to schedule for fetching
    69  
    70  	// Headers are "special", they download in batches, supported by a skeleton chain
    71  	headerHead      common.Hash                    // [eth/62] Hash of the last queued header to verify order
    72  	headerTaskPool  map[uint64]*types.Header       // [eth/62] Pending header retrieval tasks, mapping starting indexes to skeleton headers
    73  	headerTaskQueue *prque.Prque                   // [eth/62] Priority queue of the skeleton indexes to fetch the filling headers for
    74  	headerPeerMiss  map[string]map[uint64]struct{} // [eth/62] Set of per-peer header batches known to be unavailable
    75  	headerPendPool  map[string]*fetchRequest       // [eth/62] Currently pending header retrieval operations
    76  	headerResults   []*types.Header                // [eth/62] Result cache accumulating the completed headers
    77  	headerProced    int                            // [eth/62] Number of headers already processed from the results
    78  	headerOffset    uint64                         // [eth/62] Number of the first header in the result cache
    79  	headerContCh    chan bool                      // [eth/62] Channel to notify when header download finishes
    80  
    81  	// All data retrievals below are based on an already assembles header chain
    82  	blockTaskPool  map[common.Hash]*types.Header // [eth/62] Pending block (body) retrieval tasks, mapping hashes to headers
    83  	blockTaskQueue *prque.Prque                  // [eth/62] Priority queue of the headers to fetch the blocks (bodies) for
    84  	blockPendPool  map[string]*fetchRequest      // [eth/62] Currently pending block (body) retrieval operations
    85  	blockDonePool  map[common.Hash]struct{}      // [eth/62] Set of the completed block (body) fetches
    86  
    87  	receiptTaskPool  map[common.Hash]*types.Header // [eth/63] Pending receipt retrieval tasks, mapping hashes to headers
    88  	receiptTaskQueue *prque.Prque                  // [eth/63] Priority queue of the headers to fetch the receipts for
    89  	receiptPendPool  map[string]*fetchRequest      // [eth/63] Currently pending receipt retrieval operations
    90  	receiptDonePool  map[common.Hash]struct{}      // [eth/63] Set of the completed receipt fetches
    91  
    92  	resultCache  []*fetchResult     // Downloaded but not yet delivered fetch results
    93  	resultOffset uint64             // Offset of the first cached fetch result in the block chain
    94  	resultSize   common.StorageSize // Approximate size of a block (exponential moving average)
    95  
    96  	lock   *sync.Mutex
    97  	active *sync.Cond
    98  	closed bool
    99  }
   100  
   101  // newQueue creates a new download queue for scheduling block retrieval.
   102  func newQueue() *queue {
   103  	lock := new(sync.Mutex)
   104  	return &queue{
   105  		headerPendPool:   make(map[string]*fetchRequest),
   106  		headerContCh:     make(chan bool),
   107  		blockTaskPool:    make(map[common.Hash]*types.Header),
   108  		blockTaskQueue:   prque.New(),
   109  		blockPendPool:    make(map[string]*fetchRequest),
   110  		blockDonePool:    make(map[common.Hash]struct{}),
   111  		receiptTaskPool:  make(map[common.Hash]*types.Header),
   112  		receiptTaskQueue: prque.New(),
   113  		receiptPendPool:  make(map[string]*fetchRequest),
   114  		receiptDonePool:  make(map[common.Hash]struct{}),
   115  		resultCache:      make([]*fetchResult, blockCacheItems),
   116  		active:           sync.NewCond(lock),
   117  		lock:             lock,
   118  	}
   119  }
   120  
   121  // Reset clears out the queue contents.
   122  func (q *queue) Reset() {
   123  	q.lock.Lock()
   124  	defer q.lock.Unlock()
   125  
   126  	q.closed = false
   127  	q.mode = FullSync
   128  
   129  	q.headerHead = common.Hash{}
   130  	q.headerPendPool = make(map[string]*fetchRequest)
   131  
   132  	q.blockTaskPool = make(map[common.Hash]*types.Header)
   133  	q.blockTaskQueue.Reset()
   134  	q.blockPendPool = make(map[string]*fetchRequest)
   135  	q.blockDonePool = make(map[common.Hash]struct{})
   136  
   137  	q.receiptTaskPool = make(map[common.Hash]*types.Header)
   138  	q.receiptTaskQueue.Reset()
   139  	q.receiptPendPool = make(map[string]*fetchRequest)
   140  	q.receiptDonePool = make(map[common.Hash]struct{})
   141  
   142  	q.resultCache = make([]*fetchResult, blockCacheItems)
   143  	q.resultOffset = 0
   144  }
   145  
   146  // Close marks the end of the sync, unblocking WaitResults.
   147  // It may be called even if the queue is already closed.
   148  func (q *queue) Close() {
   149  	q.lock.Lock()
   150  	q.closed = true
   151  	q.lock.Unlock()
   152  	q.active.Broadcast()
   153  }
   154  
   155  // PendingHeaders retrieves the number of header requests pending for retrieval.
   156  func (q *queue) PendingHeaders() int {
   157  	q.lock.Lock()
   158  	defer q.lock.Unlock()
   159  
   160  	return q.headerTaskQueue.Size()
   161  }
   162  
   163  // PendingBlocks retrieves the number of block (body) requests pending for retrieval.
   164  func (q *queue) PendingBlocks() int {
   165  	q.lock.Lock()
   166  	defer q.lock.Unlock()
   167  
   168  	return q.blockTaskQueue.Size()
   169  }
   170  
   171  // PendingReceipts retrieves the number of block receipts pending for retrieval.
   172  func (q *queue) PendingReceipts() int {
   173  	q.lock.Lock()
   174  	defer q.lock.Unlock()
   175  
   176  	return q.receiptTaskQueue.Size()
   177  }
   178  
   179  // InFlightHeaders retrieves whether there are header fetch requests currently
   180  // in flight.
   181  func (q *queue) InFlightHeaders() bool {
   182  	q.lock.Lock()
   183  	defer q.lock.Unlock()
   184  
   185  	return len(q.headerPendPool) > 0
   186  }
   187  
   188  // InFlightBlocks retrieves whether there are block fetch requests currently in
   189  // flight.
   190  func (q *queue) InFlightBlocks() bool {
   191  	q.lock.Lock()
   192  	defer q.lock.Unlock()
   193  
   194  	return len(q.blockPendPool) > 0
   195  }
   196  
   197  // InFlightReceipts retrieves whether there are receipt fetch requests currently
   198  // in flight.
   199  func (q *queue) InFlightReceipts() bool {
   200  	q.lock.Lock()
   201  	defer q.lock.Unlock()
   202  
   203  	return len(q.receiptPendPool) > 0
   204  }
   205  
   206  // Idle returns if the queue is fully idle or has some data still inside.
   207  func (q *queue) Idle() bool {
   208  	q.lock.Lock()
   209  	defer q.lock.Unlock()
   210  
   211  	queued := q.blockTaskQueue.Size() + q.receiptTaskQueue.Size()
   212  	pending := len(q.blockPendPool) + len(q.receiptPendPool)
   213  	cached := len(q.blockDonePool) + len(q.receiptDonePool)
   214  
   215  	return (queued + pending + cached) == 0
   216  }
   217  
   218  // ShouldThrottleBlocks checks if the download should be throttled (active block (body)
   219  // fetches exceed block cache).
   220  func (q *queue) ShouldThrottleBlocks() bool {
   221  	q.lock.Lock()
   222  	defer q.lock.Unlock()
   223  
   224  	return q.resultSlots(q.blockPendPool, q.blockDonePool) <= 0
   225  }
   226  
   227  // ShouldThrottleReceipts checks if the download should be throttled (active receipt
   228  // fetches exceed block cache).
   229  func (q *queue) ShouldThrottleReceipts() bool {
   230  	q.lock.Lock()
   231  	defer q.lock.Unlock()
   232  
   233  	return q.resultSlots(q.receiptPendPool, q.receiptDonePool) <= 0
   234  }
   235  
   236  // resultSlots calculates the number of results slots available for requests
   237  // whilst adhering to both the item and the memory limit too of the results
   238  // cache.
   239  func (q *queue) resultSlots(pendPool map[string]*fetchRequest, donePool map[common.Hash]struct{}) int {
   240  	// Calculate the maximum length capped by the memory limit
   241  	limit := len(q.resultCache)
   242  	if common.StorageSize(len(q.resultCache))*q.resultSize > common.StorageSize(blockCacheMemory) {
   243  		limit = int((common.StorageSize(blockCacheMemory) + q.resultSize - 1) / q.resultSize)
   244  	}
   245  	// Calculate the number of slots already finished
   246  	finished := 0
   247  	for _, result := range q.resultCache[:limit] {
   248  		if result == nil {
   249  			break
   250  		}
   251  		if _, ok := donePool[result.Hash]; ok {
   252  			finished++
   253  		}
   254  	}
   255  	// Calculate the number of slots currently downloading
   256  	pending := 0
   257  	for _, request := range pendPool {
   258  		for _, header := range request.Headers {
   259  			if header.Number.Uint64() < q.resultOffset+uint64(limit) {
   260  				pending++
   261  			}
   262  		}
   263  	}
   264  	// Return the free slots to distribute
   265  	return limit - finished - pending
   266  }
   267  
   268  // ScheduleSkeleton adds a batch of header retrieval tasks to the queue to fill
   269  // up an already retrieved header skeleton.
   270  func (q *queue) ScheduleSkeleton(from uint64, skeleton []*types.Header) {
   271  	q.lock.Lock()
   272  	defer q.lock.Unlock()
   273  
   274  	// No skeleton retrieval can be in progress, fail hard if so (huge implementation bug)
   275  	if q.headerResults != nil {
   276  		panic("skeleton assembly already in progress")
   277  	}
   278  	// Shedule all the header retrieval tasks for the skeleton assembly
   279  	q.headerTaskPool = make(map[uint64]*types.Header)
   280  	q.headerTaskQueue = prque.New()
   281  	q.headerPeerMiss = make(map[string]map[uint64]struct{}) // Reset availability to correct invalid chains
   282  	q.headerResults = make([]*types.Header, len(skeleton)*MaxHeaderFetch)
   283  	q.headerProced = 0
   284  	q.headerOffset = from
   285  	q.headerContCh = make(chan bool, 1)
   286  
   287  	for i, header := range skeleton {
   288  		index := from + uint64(i*MaxHeaderFetch)
   289  
   290  		q.headerTaskPool[index] = header
   291  		q.headerTaskQueue.Push(index, -float32(index))
   292  	}
   293  }
   294  
   295  // RetrieveHeaders retrieves the header chain assemble based on the scheduled
   296  // skeleton.
   297  func (q *queue) RetrieveHeaders() ([]*types.Header, int) {
   298  	q.lock.Lock()
   299  	defer q.lock.Unlock()
   300  
   301  	headers, proced := q.headerResults, q.headerProced
   302  	q.headerResults, q.headerProced = nil, 0
   303  
   304  	return headers, proced
   305  }
   306  
   307  // Schedule adds a set of headers for the download queue for scheduling, returning
   308  // the new headers encountered.
   309  func (q *queue) Schedule(headers []*types.Header, from uint64) []*types.Header {
   310  	q.lock.Lock()
   311  	defer q.lock.Unlock()
   312  
   313  	// Insert all the headers prioritised by the contained block number
   314  	inserts := make([]*types.Header, 0, len(headers))
   315  	for _, header := range headers {
   316  		// Make sure chain order is honoured and preserved throughout
   317  		hash := header.Hash()
   318  		if header.Number == nil || header.Number.Uint64() != from {
   319  			log.Warn("Header broke chain ordering", "number", header.Number, "hash", hash, "expected", from)
   320  			break
   321  		}
   322  		if q.headerHead != (common.Hash{}) && q.headerHead != header.ParentHash {
   323  			log.Warn("Header broke chain ancestry", "number", header.Number, "hash", hash)
   324  			break
   325  		}
   326  		// Make sure no duplicate requests are executed
   327  		if _, ok := q.blockTaskPool[hash]; ok {
   328  			log.Warn("Header  already scheduled for block fetch", "number", header.Number, "hash", hash)
   329  			continue
   330  		}
   331  		if _, ok := q.receiptTaskPool[hash]; ok {
   332  			log.Warn("Header already scheduled for receipt fetch", "number", header.Number, "hash", hash)
   333  			continue
   334  		}
   335  		// Queue the header for content retrieval
   336  		q.blockTaskPool[hash] = header
   337  		q.blockTaskQueue.Push(header, -float32(header.Number.Uint64()))
   338  
   339  		if q.mode == FastSync {
   340  			q.receiptTaskPool[hash] = header
   341  			q.receiptTaskQueue.Push(header, -float32(header.Number.Uint64()))
   342  		}
   343  		inserts = append(inserts, header)
   344  		q.headerHead = hash
   345  		from++
   346  	}
   347  	return inserts
   348  }
   349  
   350  // Results retrieves and permanently removes a batch of fetch results from
   351  // the cache. the result slice will be empty if the queue has been closed.
   352  func (q *queue) Results(block bool) []*fetchResult {
   353  	q.lock.Lock()
   354  	defer q.lock.Unlock()
   355  
   356  	// Count the number of items available for processing
   357  	nproc := q.countProcessableItems()
   358  	for nproc == 0 && !q.closed {
   359  		if !block {
   360  			return nil
   361  		}
   362  		q.active.Wait()
   363  		nproc = q.countProcessableItems()
   364  	}
   365  	// Since we have a batch limit, don't pull more into "dangling" memory
   366  	if nproc > maxResultsProcess {
   367  		nproc = maxResultsProcess
   368  	}
   369  	results := make([]*fetchResult, nproc)
   370  	copy(results, q.resultCache[:nproc])
   371  	if len(results) > 0 {
   372  		// Mark results as done before dropping them from the cache.
   373  		for _, result := range results {
   374  			hash := result.Header.Hash()
   375  			delete(q.blockDonePool, hash)
   376  			delete(q.receiptDonePool, hash)
   377  		}
   378  		// Delete the results from the cache and clear the tail.
   379  		copy(q.resultCache, q.resultCache[nproc:])
   380  		for i := len(q.resultCache) - nproc; i < len(q.resultCache); i++ {
   381  			q.resultCache[i] = nil
   382  		}
   383  		// Advance the expected block number of the first cache entry.
   384  		q.resultOffset += uint64(nproc)
   385  
   386  		// Recalculate the result item weights to prevent memory exhaustion
   387  		for _, result := range results {
   388  			size := result.Header.Size()
   389  			for _, uncle := range result.Uncles {
   390  				size += uncle.Size()
   391  			}
   392  			for _, receipt := range result.Receipts {
   393  				size += receipt.Size()
   394  			}
   395  			for _, tx := range result.Transactions {
   396  				size += tx.Size()
   397  			}
   398  			q.resultSize = common.StorageSize(blockCacheSizeWeight)*size + (1-common.StorageSize(blockCacheSizeWeight))*q.resultSize
   399  		}
   400  	}
   401  	return results
   402  }
   403  
   404  // countProcessableItems counts the processable items.
   405  func (q *queue) countProcessableItems() int {
   406  	for i, result := range q.resultCache {
   407  		if result == nil || result.Pending > 0 {
   408  			return i
   409  		}
   410  	}
   411  	return len(q.resultCache)
   412  }
   413  
   414  // ReserveHeaders reserves a set of headers for the given peer, skipping any
   415  // previously failed batches.
   416  func (q *queue) ReserveHeaders(p *peerConnection, count int) *fetchRequest {
   417  	q.lock.Lock()
   418  	defer q.lock.Unlock()
   419  
   420  	// Short circuit if the peer's already downloading something (sanity check to
   421  	// not corrupt state)
   422  	if _, ok := q.headerPendPool[p.id]; ok {
   423  		return nil
   424  	}
   425  	// Retrieve a batch of hashes, skipping previously failed ones
   426  	send, skip := uint64(0), []uint64{}
   427  	for send == 0 && !q.headerTaskQueue.Empty() {
   428  		from, _ := q.headerTaskQueue.Pop()
   429  		if q.headerPeerMiss[p.id] != nil {
   430  			if _, ok := q.headerPeerMiss[p.id][from.(uint64)]; ok {
   431  				skip = append(skip, from.(uint64))
   432  				continue
   433  			}
   434  		}
   435  		send = from.(uint64)
   436  	}
   437  	// Merge all the skipped batches back
   438  	for _, from := range skip {
   439  		q.headerTaskQueue.Push(from, -float32(from))
   440  	}
   441  	// Assemble and return the block download request
   442  	if send == 0 {
   443  		return nil
   444  	}
   445  	request := &fetchRequest{
   446  		Peer: p,
   447  		From: send,
   448  		Time: time.Now(),
   449  	}
   450  	q.headerPendPool[p.id] = request
   451  	return request
   452  }
   453  
   454  // ReserveBodies reserves a set of body fetches for the given peer, skipping any
   455  // previously failed downloads. Beside the next batch of needed fetches, it also
   456  // returns a flag whether empty blocks were queued requiring processing.
   457  func (q *queue) ReserveBodies(p *peerConnection, count int) (*fetchRequest, bool, error) {
   458  	isNoop := func(header *types.Header) bool {
   459  		return header.TxHash == types.EmptyRootHash && header.UncleHash == types.EmptyUncleHash
   460  	}
   461  	q.lock.Lock()
   462  	defer q.lock.Unlock()
   463  
   464  	return q.reserveHeaders(p, count, q.blockTaskPool, q.blockTaskQueue, q.blockPendPool, q.blockDonePool, isNoop)
   465  }
   466  
   467  // ReserveReceipts reserves a set of receipt fetches for the given peer, skipping
   468  // any previously failed downloads. Beside the next batch of needed fetches, it
   469  // also returns a flag whether empty receipts were queued requiring importing.
   470  func (q *queue) ReserveReceipts(p *peerConnection, count int) (*fetchRequest, bool, error) {
   471  	isNoop := func(header *types.Header) bool {
   472  		return header.ReceiptHash == types.EmptyRootHash
   473  	}
   474  	q.lock.Lock()
   475  	defer q.lock.Unlock()
   476  
   477  	return q.reserveHeaders(p, count, q.receiptTaskPool, q.receiptTaskQueue, q.receiptPendPool, q.receiptDonePool, isNoop)
   478  }
   479  
   480  // reserveHeaders reserves a set of data download operations for a given peer,
   481  // skipping any previously failed ones. This method is a generic version used
   482  // by the individual special reservation functions.
   483  //
   484  // Note, this method expects the queue lock to be already held for writing. The
   485  // reason the lock is not obtained in here is because the parameters already need
   486  // to access the queue, so they already need a lock anyway.
   487  func (q *queue) reserveHeaders(p *peerConnection, count int, taskPool map[common.Hash]*types.Header, taskQueue *prque.Prque,
   488  	pendPool map[string]*fetchRequest, donePool map[common.Hash]struct{}, isNoop func(*types.Header) bool) (*fetchRequest, bool, error) {
   489  	// Short circuit if the pool has been depleted, or if the peer's already
   490  	// downloading something (sanity check not to corrupt state)
   491  	if taskQueue.Empty() {
   492  		return nil, false, nil
   493  	}
   494  	if _, ok := pendPool[p.id]; ok {
   495  		return nil, false, nil
   496  	}
   497  	// Calculate an upper limit on the items we might fetch (i.e. throttling)
   498  	space := q.resultSlots(pendPool, donePool)
   499  
   500  	// Retrieve a batch of tasks, skipping previously failed ones
   501  	send := make([]*types.Header, 0, count)
   502  	skip := make([]*types.Header, 0)
   503  
   504  	progress := false
   505  	for proc := 0; proc < space && len(send) < count && !taskQueue.Empty(); proc++ {
   506  		header := taskQueue.PopItem().(*types.Header)
   507  		hash := header.Hash()
   508  
   509  		// If we're the first to request this task, initialise the result container
   510  		index := int(header.Number.Int64() - int64(q.resultOffset))
   511  		if index >= len(q.resultCache) || index < 0 {
   512  			common.Report("index allocation went beyond available resultCache space")
   513  			return nil, false, errInvalidChain
   514  		}
   515  		if q.resultCache[index] == nil {
   516  			components := 1
   517  			if q.mode == FastSync {
   518  				components = 2
   519  			}
   520  			q.resultCache[index] = &fetchResult{
   521  				Pending: components,
   522  				Hash:    hash,
   523  				Header:  header,
   524  			}
   525  		}
   526  		// If this fetch task is a noop, skip this fetch operation
   527  		if isNoop(header) {
   528  			donePool[hash] = struct{}{}
   529  			delete(taskPool, hash)
   530  
   531  			space, proc = space-1, proc-1
   532  			q.resultCache[index].Pending--
   533  			progress = true
   534  			continue
   535  		}
   536  		// Otherwise unless the peer is known not to have the data, add to the retrieve list
   537  		if p.Lacks(hash) {
   538  			skip = append(skip, header)
   539  		} else {
   540  			send = append(send, header)
   541  		}
   542  	}
   543  	// Merge all the skipped headers back
   544  	for _, header := range skip {
   545  		taskQueue.Push(header, -float32(header.Number.Uint64()))
   546  	}
   547  	if progress {
   548  		// Wake WaitResults, resultCache was modified
   549  		q.active.Signal()
   550  	}
   551  	// Assemble and return the block download request
   552  	if len(send) == 0 {
   553  		return nil, progress, nil
   554  	}
   555  	request := &fetchRequest{
   556  		Peer:    p,
   557  		Headers: send,
   558  		Time:    time.Now(),
   559  	}
   560  	pendPool[p.id] = request
   561  
   562  	return request, progress, nil
   563  }
   564  
   565  // CancelHeaders aborts a fetch request, returning all pending skeleton indexes to the queue.
   566  func (q *queue) CancelHeaders(request *fetchRequest) {
   567  	q.cancel(request, q.headerTaskQueue, q.headerPendPool)
   568  }
   569  
   570  // CancelBodies aborts a body fetch request, returning all pending headers to the
   571  // task queue.
   572  func (q *queue) CancelBodies(request *fetchRequest) {
   573  	q.cancel(request, q.blockTaskQueue, q.blockPendPool)
   574  }
   575  
   576  // CancelReceipts aborts a body fetch request, returning all pending headers to
   577  // the task queue.
   578  func (q *queue) CancelReceipts(request *fetchRequest) {
   579  	q.cancel(request, q.receiptTaskQueue, q.receiptPendPool)
   580  }
   581  
   582  // Cancel aborts a fetch request, returning all pending hashes to the task queue.
   583  func (q *queue) cancel(request *fetchRequest, taskQueue *prque.Prque, pendPool map[string]*fetchRequest) {
   584  	q.lock.Lock()
   585  	defer q.lock.Unlock()
   586  
   587  	if request.From > 0 {
   588  		taskQueue.Push(request.From, -float32(request.From))
   589  	}
   590  	for _, header := range request.Headers {
   591  		taskQueue.Push(header, -float32(header.Number.Uint64()))
   592  	}
   593  	delete(pendPool, request.Peer.id)
   594  }
   595  
   596  // Revoke cancels all pending requests belonging to a given peer. This method is
   597  // meant to be called during a peer drop to quickly reassign owned data fetches
   598  // to remaining nodes.
   599  func (q *queue) Revoke(peerId string) {
   600  	q.lock.Lock()
   601  	defer q.lock.Unlock()
   602  
   603  	if request, ok := q.blockPendPool[peerId]; ok {
   604  		for _, header := range request.Headers {
   605  			q.blockTaskQueue.Push(header, -float32(header.Number.Uint64()))
   606  		}
   607  		delete(q.blockPendPool, peerId)
   608  	}
   609  	if request, ok := q.receiptPendPool[peerId]; ok {
   610  		for _, header := range request.Headers {
   611  			q.receiptTaskQueue.Push(header, -float32(header.Number.Uint64()))
   612  		}
   613  		delete(q.receiptPendPool, peerId)
   614  	}
   615  }
   616  
   617  // ExpireHeaders checks for in flight requests that exceeded a timeout allowance,
   618  // canceling them and returning the responsible peers for penalisation.
   619  func (q *queue) ExpireHeaders(timeout time.Duration) map[string]int {
   620  	q.lock.Lock()
   621  	defer q.lock.Unlock()
   622  
   623  	return q.expire(timeout, q.headerPendPool, q.headerTaskQueue, headerTimeoutMeter)
   624  }
   625  
   626  // ExpireBodies checks for in flight block body requests that exceeded a timeout
   627  // allowance, canceling them and returning the responsible peers for penalisation.
   628  func (q *queue) ExpireBodies(timeout time.Duration) map[string]int {
   629  	q.lock.Lock()
   630  	defer q.lock.Unlock()
   631  
   632  	return q.expire(timeout, q.blockPendPool, q.blockTaskQueue, bodyTimeoutMeter)
   633  }
   634  
   635  // ExpireReceipts checks for in flight receipt requests that exceeded a timeout
   636  // allowance, canceling them and returning the responsible peers for penalisation.
   637  func (q *queue) ExpireReceipts(timeout time.Duration) map[string]int {
   638  	q.lock.Lock()
   639  	defer q.lock.Unlock()
   640  
   641  	return q.expire(timeout, q.receiptPendPool, q.receiptTaskQueue, receiptTimeoutMeter)
   642  }
   643  
   644  // expire is the generic check that move expired tasks from a pending pool back
   645  // into a task pool, returning all entities caught with expired tasks.
   646  //
   647  // Note, this method expects the queue lock to be already held. The
   648  // reason the lock is not obtained in here is because the parameters already need
   649  // to access the queue, so they already need a lock anyway.
   650  func (q *queue) expire(timeout time.Duration, pendPool map[string]*fetchRequest, taskQueue *prque.Prque, timeoutMeter metrics.Meter) map[string]int {
   651  	// Iterate over the expired requests and return each to the queue
   652  	expiries := make(map[string]int)
   653  	for id, request := range pendPool {
   654  		if time.Since(request.Time) > timeout {
   655  			// Update the metrics with the timeout
   656  			timeoutMeter.Mark(1)
   657  
   658  			// Return any non satisfied requests to the pool
   659  			if request.From > 0 {
   660  				taskQueue.Push(request.From, -float32(request.From))
   661  			}
   662  			for _, header := range request.Headers {
   663  				taskQueue.Push(header, -float32(header.Number.Uint64()))
   664  			}
   665  			// Add the peer to the expiry report along the the number of failed requests
   666  			expiries[id] = len(request.Headers)
   667  		}
   668  	}
   669  	// Remove the expired requests from the pending pool
   670  	for id := range expiries {
   671  		delete(pendPool, id)
   672  	}
   673  	return expiries
   674  }
   675  
   676  // DeliverHeaders injects a header retrieval response into the header results
   677  // cache. This method either accepts all headers it received, or none of them
   678  // if they do not map correctly to the skeleton.
   679  //
   680  // If the headers are accepted, the method makes an attempt to deliver the set
   681  // of ready headers to the processor to keep the pipeline full. However it will
   682  // not block to prevent stalling other pending deliveries.
   683  func (q *queue) DeliverHeaders(id string, headers []*types.Header, headerProcCh chan []*types.Header) (int, error) {
   684  	q.lock.Lock()
   685  	defer q.lock.Unlock()
   686  
   687  	// Short circuit if the data was never requested
   688  	request := q.headerPendPool[id]
   689  	if request == nil {
   690  		return 0, errNoFetchesPending
   691  	}
   692  	headerReqTimer.UpdateSince(request.Time)
   693  	delete(q.headerPendPool, id)
   694  
   695  	// Ensure headers can be mapped onto the skeleton chain
   696  	target := q.headerTaskPool[request.From].Hash()
   697  
   698  	accepted := len(headers) == MaxHeaderFetch
   699  	if accepted {
   700  		if headers[0].Number.Uint64() != request.From {
   701  			log.Trace("First header broke chain ordering", "peer", id, "number", headers[0].Number, "hash", headers[0].Hash(), request.From)
   702  			accepted = false
   703  		} else if headers[len(headers)-1].Hash() != target {
   704  			log.Trace("Last header broke skeleton structure ", "peer", id, "number", headers[len(headers)-1].Number, "hash", headers[len(headers)-1].Hash(), "expected", target)
   705  			accepted = false
   706  		}
   707  	}
   708  	if accepted {
   709  		for i, header := range headers[1:] {
   710  			hash := header.Hash()
   711  			if want := request.From + 1 + uint64(i); header.Number.Uint64() != want {
   712  				log.Warn("Header broke chain ordering", "peer", id, "number", header.Number, "hash", hash, "expected", want)
   713  				accepted = false
   714  				break
   715  			}
   716  			if headers[i].Hash() != header.ParentHash {
   717  				log.Warn("Header broke chain ancestry", "peer", id, "number", header.Number, "hash", hash)
   718  				accepted = false
   719  				break
   720  			}
   721  		}
   722  	}
   723  	// If the batch of headers wasn't accepted, mark as unavailable
   724  	if !accepted {
   725  		log.Trace("Skeleton filling not accepted", "peer", id, "from", request.From)
   726  
   727  		miss := q.headerPeerMiss[id]
   728  		if miss == nil {
   729  			q.headerPeerMiss[id] = make(map[uint64]struct{})
   730  			miss = q.headerPeerMiss[id]
   731  		}
   732  		miss[request.From] = struct{}{}
   733  
   734  		q.headerTaskQueue.Push(request.From, -float32(request.From))
   735  		return 0, errors.New("delivery not accepted")
   736  	}
   737  	// Clean up a successful fetch and try to deliver any sub-results
   738  	copy(q.headerResults[request.From-q.headerOffset:], headers)
   739  	delete(q.headerTaskPool, request.From)
   740  
   741  	ready := 0
   742  	for q.headerProced+ready < len(q.headerResults) && q.headerResults[q.headerProced+ready] != nil {
   743  		ready += MaxHeaderFetch
   744  	}
   745  	if ready > 0 {
   746  		// Headers are ready for delivery, gather them and push forward (non blocking)
   747  		process := make([]*types.Header, ready)
   748  		copy(process, q.headerResults[q.headerProced:q.headerProced+ready])
   749  
   750  		select {
   751  		case headerProcCh <- process:
   752  			log.Trace("Pre-scheduled new headers", "peer", id, "count", len(process), "from", process[0].Number)
   753  			q.headerProced += len(process)
   754  		default:
   755  		}
   756  	}
   757  	// Check for termination and return
   758  	if len(q.headerTaskPool) == 0 {
   759  		q.headerContCh <- false
   760  	}
   761  	return len(headers), nil
   762  }
   763  
   764  // DeliverBodies injects a block body retrieval response into the results queue.
   765  // The method returns the number of blocks bodies accepted from the delivery and
   766  // also wakes any threads waiting for data delivery.
   767  func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, uncleLists [][]*types.Header) (int, error) {
   768  	q.lock.Lock()
   769  	defer q.lock.Unlock()
   770  
   771  	reconstruct := func(header *types.Header, index int, result *fetchResult) error {
   772  		if types.DeriveSha(types.Transactions(txLists[index])) != header.TxHash || types.CalcUncleHash(uncleLists[index]) != header.UncleHash {
   773  			return errInvalidBody
   774  		}
   775  		result.Transactions = txLists[index]
   776  		result.Uncles = uncleLists[index]
   777  		return nil
   778  	}
   779  	return q.deliver(id, q.blockTaskPool, q.blockTaskQueue, q.blockPendPool, q.blockDonePool, bodyReqTimer, len(txLists), reconstruct)
   780  }
   781  
   782  // DeliverReceipts injects a receipt retrieval response into the results queue.
   783  // The method returns the number of transaction receipts accepted from the delivery
   784  // and also wakes any threads waiting for data delivery.
   785  func (q *queue) DeliverReceipts(id string, receiptList [][]*types.Receipt) (int, error) {
   786  	q.lock.Lock()
   787  	defer q.lock.Unlock()
   788  
   789  	reconstruct := func(header *types.Header, index int, result *fetchResult) error {
   790  		if types.DeriveSha(types.Receipts(receiptList[index])) != header.ReceiptHash {
   791  			return errInvalidReceipt
   792  		}
   793  		result.Receipts = receiptList[index]
   794  		return nil
   795  	}
   796  	return q.deliver(id, q.receiptTaskPool, q.receiptTaskQueue, q.receiptPendPool, q.receiptDonePool, receiptReqTimer, len(receiptList), reconstruct)
   797  }
   798  
   799  // deliver injects a data retrieval response into the results queue.
   800  //
   801  // Note, this method expects the queue lock to be already held for writing. The
   802  // reason the lock is not obtained in here is because the parameters already need
   803  // to access the queue, so they already need a lock anyway.
   804  func (q *queue) deliver(id string, taskPool map[common.Hash]*types.Header, taskQueue *prque.Prque,
   805  	pendPool map[string]*fetchRequest, donePool map[common.Hash]struct{}, reqTimer metrics.Timer,
   806  	results int, reconstruct func(header *types.Header, index int, result *fetchResult) error) (int, error) {
   807  
   808  	// Short circuit if the data was never requested
   809  	request := pendPool[id]
   810  	if request == nil {
   811  		return 0, errNoFetchesPending
   812  	}
   813  	reqTimer.UpdateSince(request.Time)
   814  	delete(pendPool, id)
   815  
   816  	// If no data items were retrieved, mark them as unavailable for the origin peer
   817  	if results == 0 {
   818  		for _, header := range request.Headers {
   819  			request.Peer.MarkLacking(header.Hash())
   820  		}
   821  	}
   822  	// Assemble each of the results with their headers and retrieved data parts
   823  	var (
   824  		accepted int
   825  		failure  error
   826  		useful   bool
   827  	)
   828  	for i, header := range request.Headers {
   829  		// Short circuit assembly if no more fetch results are found
   830  		if i >= results {
   831  			break
   832  		}
   833  		// Reconstruct the next result if contents match up
   834  		index := int(header.Number.Int64() - int64(q.resultOffset))
   835  		if index >= len(q.resultCache) || index < 0 || q.resultCache[index] == nil {
   836  			failure = errInvalidChain
   837  			break
   838  		}
   839  		if err := reconstruct(header, i, q.resultCache[index]); err != nil {
   840  			failure = err
   841  			break
   842  		}
   843  		hash := header.Hash()
   844  
   845  		donePool[hash] = struct{}{}
   846  		q.resultCache[index].Pending--
   847  		useful = true
   848  		accepted++
   849  
   850  		// Clean up a successful fetch
   851  		request.Headers[i] = nil
   852  		delete(taskPool, hash)
   853  	}
   854  	// Return all failed or missing fetches to the queue
   855  	for _, header := range request.Headers {
   856  		if header != nil {
   857  			taskQueue.Push(header, -float32(header.Number.Uint64()))
   858  		}
   859  	}
   860  	// Wake up WaitResults
   861  	if accepted > 0 {
   862  		q.active.Signal()
   863  	}
   864  	// If none of the data was good, it's a stale delivery
   865  	switch {
   866  	case failure == nil || failure == errInvalidChain:
   867  		return accepted, failure
   868  	case useful:
   869  		return accepted, fmt.Errorf("partial failure: %v", failure)
   870  	default:
   871  		return accepted, errStaleDelivery
   872  	}
   873  }
   874  
   875  // Prepare configures the result cache to allow accepting and caching inbound
   876  // fetch results.
   877  func (q *queue) Prepare(offset uint64, mode SyncMode) {
   878  	q.lock.Lock()
   879  	defer q.lock.Unlock()
   880  
   881  	// Prepare the queue for sync results
   882  	if q.resultOffset < offset {
   883  		q.resultOffset = offset
   884  	}
   885  	q.mode = mode
   886  }