fetchers.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/core/types"
  21. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  22. )
  23. // fetchHeadersByHash is a blocking version of Peer.RequestHeadersByHash which
  24. // handles all the cancellation, interruption and timeout mechanisms of a data
  25. // retrieval to allow blocking API calls.
  26. func (d *Downloader) fetchHeadersByHash(p *peerConnection, hash common.Hash, amount int, skip int, reverse bool) ([]*types.Header, []common.Hash, error) {
  27. // Create the response sink and send the network request
  28. start := time.Now()
  29. resCh := make(chan *eth.Response)
  30. req, err := p.peer.RequestHeadersByHash(hash, amount, skip, reverse, resCh)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. defer req.Close()
  35. // Wait until the response arrives, the request is cancelled or times out
  36. ttl := d.peers.rates.TargetTimeout()
  37. timeoutTimer := time.NewTimer(ttl)
  38. defer timeoutTimer.Stop()
  39. select {
  40. case <-d.cancelCh:
  41. return nil, nil, errCanceled
  42. case <-timeoutTimer.C:
  43. // Header retrieval timed out, update the metrics
  44. p.log.Debug("Header request timed out", "elapsed", ttl)
  45. headerTimeoutMeter.Mark(1)
  46. return nil, nil, errTimeout
  47. case res := <-resCh:
  48. // Headers successfully retrieved, update the metrics
  49. headerReqTimer.Update(time.Since(start))
  50. headerInMeter.Mark(int64(len(*res.Res.(*eth.BlockHeadersPacket))))
  51. // Don't reject the packet even if it turns out to be bad, downloader will
  52. // disconnect the peer on its own terms. Simply delivery the headers to
  53. // be processed by the caller
  54. res.Done <- nil
  55. return *res.Res.(*eth.BlockHeadersPacket), res.Meta.([]common.Hash), nil
  56. }
  57. }
  58. // fetchHeadersByNumber is a blocking version of Peer.RequestHeadersByNumber which
  59. // handles all the cancellation, interruption and timeout mechanisms of a data
  60. // retrieval to allow blocking API calls.
  61. func (d *Downloader) fetchHeadersByNumber(p *peerConnection, number uint64, amount int, skip int, reverse bool) ([]*types.Header, []common.Hash, error) {
  62. // Create the response sink and send the network request
  63. start := time.Now()
  64. resCh := make(chan *eth.Response)
  65. req, err := p.peer.RequestHeadersByNumber(number, amount, skip, reverse, resCh)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. defer req.Close()
  70. // Wait until the response arrives, the request is cancelled or times out
  71. ttl := d.peers.rates.TargetTimeout()
  72. timeoutTimer := time.NewTimer(ttl)
  73. defer timeoutTimer.Stop()
  74. select {
  75. case <-d.cancelCh:
  76. return nil, nil, errCanceled
  77. case <-timeoutTimer.C:
  78. // Header retrieval timed out, update the metrics
  79. p.log.Debug("Header request timed out", "elapsed", ttl)
  80. headerTimeoutMeter.Mark(1)
  81. return nil, nil, errTimeout
  82. case res := <-resCh:
  83. // Headers successfully retrieved, update the metrics
  84. headerReqTimer.Update(time.Since(start))
  85. headerInMeter.Mark(int64(len(*res.Res.(*eth.BlockHeadersPacket))))
  86. // Don't reject the packet even if it turns out to be bad, downloader will
  87. // disconnect the peer on its own terms. Simply delivery the headers to
  88. // be processed by the caller
  89. res.Done <- nil
  90. return *res.Res.(*eth.BlockHeadersPacket), res.Meta.([]common.Hash), nil
  91. }
  92. }