handler_diff.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "github.com/ethereum/go-ethereum/core"
  20. "github.com/ethereum/go-ethereum/eth/protocols/diff"
  21. "github.com/ethereum/go-ethereum/p2p/enode"
  22. )
  23. // diffHandler implements the diff.Backend interface to handle the various network
  24. // packets that are sent as replies or broadcasts.
  25. type diffHandler handler
  26. func (h *diffHandler) Chain() *core.BlockChain { return h.chain }
  27. // RunPeer is invoked when a peer joins on the `diff` protocol.
  28. func (h *diffHandler) RunPeer(peer *diff.Peer, hand diff.Handler) error {
  29. if err := peer.Handshake(h.diffSync); err != nil {
  30. // ensure that waitDiffExtension receives the exit signal normally
  31. // otherwise, can't graceful shutdown
  32. ps := h.peers
  33. id := peer.ID()
  34. // Ensure nobody can double connect
  35. ps.lock.Lock()
  36. if wait, ok := ps.diffWait[id]; ok {
  37. delete(ps.diffWait, id)
  38. wait <- peer
  39. }
  40. ps.lock.Unlock()
  41. peer.Close()
  42. return err
  43. }
  44. return (*handler)(h).runDiffExtension(peer, hand)
  45. }
  46. // PeerInfo retrieves all known `diff` information about a peer.
  47. func (h *diffHandler) PeerInfo(id enode.ID) interface{} {
  48. if p := h.peers.peer(id.String()); p != nil && p.diffExt != nil {
  49. return p.diffExt.info()
  50. }
  51. return nil
  52. }
  53. // Handle is invoked from a peer's message handler when it receives a new remote
  54. // message that the handler couldn't consume and serve itself.
  55. func (h *diffHandler) Handle(peer *diff.Peer, packet diff.Packet) error {
  56. // DeliverSnapPacket is invoked from a peer's message handler when it transmits a
  57. // data packet for the local node to consume.
  58. switch packet := packet.(type) {
  59. case *diff.DiffLayersPacket:
  60. return h.handleDiffLayerPackage(packet, peer.ID(), false)
  61. case *diff.FullDiffLayersPacket:
  62. return h.handleDiffLayerPackage(&packet.DiffLayersPacket, peer.ID(), true)
  63. default:
  64. return fmt.Errorf("unexpected diff packet type: %T", packet)
  65. }
  66. }
  67. func (h *diffHandler) handleDiffLayerPackage(packet *diff.DiffLayersPacket, pid string, fulfilled bool) error {
  68. diffs, err := packet.Unpack()
  69. if err != nil {
  70. return err
  71. }
  72. for _, d := range diffs {
  73. if d != nil {
  74. if err := d.Validate(); err != nil {
  75. return err
  76. }
  77. }
  78. }
  79. for _, diff := range diffs {
  80. err := h.chain.HandleDiffLayer(diff, pid, fulfilled)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. return nil
  86. }