handler_eth.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2020 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 eth
  17. import (
  18. "fmt"
  19. "math/big"
  20. "sync/atomic"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  26. "github.com/ethereum/go-ethereum/p2p/enode"
  27. )
  28. // ethHandler implements the eth.Backend interface to handle the various network
  29. // packets that are sent as replies or broadcasts.
  30. type ethHandler handler
  31. func (h *ethHandler) Chain() *core.BlockChain { return h.chain }
  32. func (h *ethHandler) TxPool() eth.TxPool { return h.txpool }
  33. // RunPeer is invoked when a peer joins on the `eth` protocol.
  34. func (h *ethHandler) RunPeer(peer *eth.Peer, hand eth.Handler) error {
  35. return (*handler)(h).runEthPeer(peer, hand)
  36. }
  37. // PeerInfo retrieves all known `eth` information about a peer.
  38. func (h *ethHandler) PeerInfo(id enode.ID) interface{} {
  39. if p := h.peers.peer(id.String()); p != nil {
  40. return p.info()
  41. }
  42. return nil
  43. }
  44. // AcceptTxs retrieves whether transaction processing is enabled on the node
  45. // or if inbound transactions should simply be dropped.
  46. func (h *ethHandler) AcceptTxs() bool {
  47. return atomic.LoadUint32(&h.acceptTxs) == 1
  48. }
  49. // Handle is invoked from a peer's message handler when it receives a new remote
  50. // message that the handler couldn't consume and serve itself.
  51. func (h *ethHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
  52. // Consume any broadcasts and announces, forwarding the rest to the downloader
  53. switch packet := packet.(type) {
  54. case *eth.NewBlockHashesPacket:
  55. hashes, numbers := packet.Unpack()
  56. return h.handleBlockAnnounces(peer, hashes, numbers)
  57. case *eth.NewBlockPacket:
  58. return h.handleBlockBroadcast(peer, packet.Block, packet.TD)
  59. case *eth.NewPooledTransactionHashesPacket:
  60. return h.txFetcher.Notify(peer.ID(), *packet)
  61. case *eth.TransactionsPacket:
  62. return h.txFetcher.Enqueue(peer.ID(), *packet, false)
  63. case *eth.PooledTransactionsPacket:
  64. return h.txFetcher.Enqueue(peer.ID(), *packet, true)
  65. default:
  66. return fmt.Errorf("unexpected eth packet type: %T", packet)
  67. }
  68. }
  69. // handleBlockAnnounces is invoked from a peer's message handler when it transmits a
  70. // batch of block announcements for the local node to process.
  71. func (h *ethHandler) handleBlockAnnounces(peer *eth.Peer, hashes []common.Hash, numbers []uint64) error {
  72. // Drop all incoming block announces from the p2p network if
  73. // the chain already entered the pos stage and disconnect the
  74. // remote peer.
  75. if h.merger.PoSFinalized() {
  76. // TODO (MariusVanDerWijden) drop non-updated peers after the merge
  77. return nil
  78. // return errors.New("unexpected block announces")
  79. }
  80. // Schedule all the unknown hashes for retrieval
  81. var (
  82. unknownHashes = make([]common.Hash, 0, len(hashes))
  83. unknownNumbers = make([]uint64, 0, len(numbers))
  84. )
  85. for i := 0; i < len(hashes); i++ {
  86. if !h.chain.HasBlock(hashes[i], numbers[i]) {
  87. unknownHashes = append(unknownHashes, hashes[i])
  88. unknownNumbers = append(unknownNumbers, numbers[i])
  89. }
  90. }
  91. for i := 0; i < len(unknownHashes); i++ {
  92. h.blockFetcher.Notify(peer.ID(), unknownHashes[i], unknownNumbers[i], time.Now(), peer.RequestOneHeader, peer.RequestBodies)
  93. }
  94. return nil
  95. }
  96. // handleBlockBroadcast is invoked from a peer's message handler when it transmits a
  97. // block broadcast for the local node to process.
  98. func (h *ethHandler) handleBlockBroadcast(peer *eth.Peer, block *types.Block, td *big.Int) error {
  99. // Drop all incoming block announces from the p2p network if
  100. // the chain already entered the pos stage and disconnect the
  101. // remote peer.
  102. if h.merger.PoSFinalized() {
  103. // TODO (MariusVanDerWijden) drop non-updated peers after the merge
  104. return nil
  105. // return errors.New("unexpected block announces")
  106. }
  107. // Schedule the block for import
  108. h.blockFetcher.Enqueue(peer.ID(), block)
  109. // Assuming the block is importable by the peer, but possibly not yet done so,
  110. // calculate the head hash and TD that the peer truly must have.
  111. var (
  112. trueHead = block.ParentHash()
  113. trueTD = new(big.Int).Sub(td, block.Difficulty())
  114. )
  115. // Update the peer's total difficulty if better than the previous
  116. if _, td := peer.Head(); trueTD.Cmp(td) > 0 {
  117. peer.SetHead(trueHead, trueTD)
  118. h.chainSync.handlePeerEvent(peer)
  119. }
  120. return nil
  121. }