fetchers_concurrent_receipts.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. package downloader
  17. import (
  18. "time"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  21. "github.com/ethereum/go-ethereum/log"
  22. )
  23. // receiptQueue implements typedQueue and is a type adapter between the generic
  24. // concurrent fetcher and the downloader.
  25. type receiptQueue Downloader
  26. // waker returns a notification channel that gets pinged in case more receipt
  27. // fetches have been queued up, so the fetcher might assign it to idle peers.
  28. func (q *receiptQueue) waker() chan bool {
  29. return q.queue.receiptWakeCh
  30. }
  31. // pending returns the number of receipt that are currently queued for fetching
  32. // by the concurrent downloader.
  33. func (q *receiptQueue) pending() int {
  34. return q.queue.PendingReceipts()
  35. }
  36. // capacity is responsible for calculating how many receipts a particular peer is
  37. // estimated to be able to retrieve within the allotted round trip time.
  38. func (q *receiptQueue) capacity(peer *peerConnection, rtt time.Duration) int {
  39. return peer.ReceiptCapacity(rtt)
  40. }
  41. // updateCapacity is responsible for updating how many receipts a particular peer
  42. // is estimated to be able to retrieve in a unit time.
  43. func (q *receiptQueue) updateCapacity(peer *peerConnection, items int, span time.Duration) {
  44. peer.UpdateReceiptRate(items, span)
  45. }
  46. // reserve is responsible for allocating a requested number of pending receipts
  47. // from the download queue to the specified peer.
  48. func (q *receiptQueue) reserve(peer *peerConnection, items int) (*fetchRequest, bool, bool) {
  49. return q.queue.ReserveReceipts(peer, items)
  50. }
  51. // unreserve is responsible for removing the current receipt retrieval allocation
  52. // assigned to a specific peer and placing it back into the pool to allow
  53. // reassigning to some other peer.
  54. func (q *receiptQueue) unreserve(peer string) int {
  55. fails := q.queue.ExpireReceipts(peer)
  56. if fails > 2 {
  57. log.Trace("Receipt delivery timed out", "peer", peer)
  58. } else {
  59. log.Debug("Receipt delivery stalling", "peer", peer)
  60. }
  61. return fails
  62. }
  63. // request is responsible for converting a generic fetch request into a receipt
  64. // one and sending it to the remote peer for fulfillment.
  65. func (q *receiptQueue) request(peer *peerConnection, req *fetchRequest, resCh chan *eth.Response) (*eth.Request, error) {
  66. peer.log.Trace("Requesting new batch of receipts", "count", len(req.Headers), "from", req.Headers[0].Number)
  67. if q.receiptFetchHook != nil {
  68. q.receiptFetchHook(req.Headers)
  69. }
  70. hashes := make([]common.Hash, 0, len(req.Headers))
  71. for _, header := range req.Headers {
  72. hashes = append(hashes, header.Hash())
  73. }
  74. return peer.peer.RequestReceipts(hashes, resCh)
  75. }
  76. // deliver is responsible for taking a generic response packet from the concurrent
  77. // fetcher, unpacking the receipt data and delivering it to the downloader's queue.
  78. func (q *receiptQueue) deliver(peer *peerConnection, packet *eth.Response) (int, error) {
  79. receipts := *packet.Res.(*eth.ReceiptsPacket)
  80. hashes := packet.Meta.([]common.Hash) // {receipt hashes}
  81. accepted, err := q.queue.DeliverReceipts(peer.id, receipts, hashes)
  82. switch {
  83. case err == nil && len(receipts) == 0:
  84. peer.log.Trace("Requested receipts delivered")
  85. case err == nil:
  86. peer.log.Trace("Delivered new batch of receipts", "count", len(receipts), "accepted", accepted)
  87. default:
  88. peer.log.Debug("Failed to deliver retrieved receipts", "err", err)
  89. }
  90. return accepted, err
  91. }