github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/eth/downloader/fetchers_concurrent_receipts.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package downloader
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/eth/protocols/eth"
    24  	"github.com/ethereum/go-ethereum/log"
    25  )
    26  
    27  // receiptQueue implements typedQueue and is a type adapter between the generic
    28  // concurrent fetcher and the downloader.
    29  type receiptQueue Downloader
    30  
    31  // waker returns a notification channel that gets pinged in case more reecipt
    32  // fetches have been queued up, so the fetcher might assign it to idle peers.
    33  func (q *receiptQueue) waker() chan bool {
    34  	return q.queue.receiptWakeCh
    35  }
    36  
    37  // pending returns the number of receipt that are currently queued for fetching
    38  // by the concurrent downloader.
    39  func (q *receiptQueue) pending() int {
    40  	return q.queue.PendingReceipts()
    41  }
    42  
    43  // capacity is responsible for calculating how many receipts a particular peer is
    44  // estimated to be able to retrieve within the alloted round trip time.
    45  func (q *receiptQueue) capacity(peer *peerConnection, rtt time.Duration) int {
    46  	return peer.ReceiptCapacity(rtt)
    47  }
    48  
    49  // updateCapacity is responsible for updating how many receipts a particular peer
    50  // is estimated to be able to retrieve in a unit time.
    51  func (q *receiptQueue) updateCapacity(peer *peerConnection, items int, span time.Duration) {
    52  	peer.UpdateReceiptRate(items, span)
    53  }
    54  
    55  // reserve is responsible for allocating a requested number of pending receipts
    56  // from the download queue to the specified peer.
    57  func (q *receiptQueue) reserve(peer *peerConnection, items int) (*fetchRequest, bool, bool) {
    58  	return q.queue.ReserveReceipts(peer, items)
    59  }
    60  
    61  // unreserve is resposible for removing the current receipt retrieval allocation
    62  // assigned to a specific peer and placing it back into the pool to allow
    63  // reassigning to some other peer.
    64  func (q *receiptQueue) unreserve(peer string) int {
    65  	fails := q.queue.ExpireReceipts(peer)
    66  	if fails > 2 {
    67  		log.Trace("Receipt delivery timed out", "peer", peer)
    68  	} else {
    69  		log.Debug("Receipt delivery stalling", "peer", peer)
    70  	}
    71  	return fails
    72  }
    73  
    74  // request is responsible for converting a generic fetch request into a receipt
    75  // one and sending it to the remote peer for fulfillment.
    76  func (q *receiptQueue) request(peer *peerConnection, req *fetchRequest, resCh chan *eth.Response) (*eth.Request, error) {
    77  	peer.log.Trace("Requesting new batch of receipts", "count", len(req.Headers), "from", req.Headers[0].Number)
    78  	if q.receiptFetchHook != nil {
    79  		q.receiptFetchHook(req.Headers)
    80  	}
    81  	hashes := make([]common.Hash, 0, len(req.Headers))
    82  	for _, header := range req.Headers {
    83  		hashes = append(hashes, header.Hash())
    84  	}
    85  	return peer.peer.RequestReceipts(hashes, resCh)
    86  }
    87  
    88  // deliver is responsible for taking a generic response packet from the concurrent
    89  // fetcher, unpacking the receipt data and delivering it to the downloader's queue.
    90  func (q *receiptQueue) deliver(peer *peerConnection, packet *eth.Response) (int, error) {
    91  	receipts := *packet.Res.(*eth.ReceiptsPacket)
    92  	hashes := packet.Meta.([]common.Hash) // {receipt hashes}
    93  
    94  	accepted, err := q.queue.DeliverReceipts(peer.id, receipts, hashes)
    95  	switch {
    96  	case err == nil && len(receipts) == 0:
    97  		peer.log.Trace("Requested receipts delivered")
    98  	case err == nil:
    99  		peer.log.Trace("Delivered new batch of receipts", "count", len(receipts), "accepted", accepted)
   100  	default:
   101  		peer.log.Debug("Failed to deliver retrieved receipts", "err", err)
   102  	}
   103  	return accepted, err
   104  }