handler_diff.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return err
  42. }
  43. return (*handler)(h).runDiffExtension(peer, hand)
  44. }
  45. // PeerInfo retrieves all known `diff` information about a peer.
  46. func (h *diffHandler) PeerInfo(id enode.ID) interface{} {
  47. if p := h.peers.peer(id.String()); p != nil && p.diffExt != nil {
  48. return p.diffExt.info()
  49. }
  50. return nil
  51. }
  52. // Handle is invoked from a peer's message handler when it receives a new remote
  53. // message that the handler couldn't consume and serve itself.
  54. func (h *diffHandler) Handle(peer *diff.Peer, packet diff.Packet) error {
  55. // DeliverSnapPacket is invoked from a peer's message handler when it transmits a
  56. // data packet for the local node to consume.
  57. switch packet := packet.(type) {
  58. case *diff.DiffLayersPacket:
  59. return h.handleDiffLayerPackage(packet, peer.ID(), false)
  60. case *diff.FullDiffLayersPacket:
  61. return h.handleDiffLayerPackage(&packet.DiffLayersPacket, peer.ID(), true)
  62. default:
  63. return fmt.Errorf("unexpected diff packet type: %T", packet)
  64. }
  65. }
  66. func (h *diffHandler) handleDiffLayerPackage(packet *diff.DiffLayersPacket, pid string, fulfilled bool) error {
  67. diffs, err := packet.Unpack()
  68. if err != nil {
  69. return err
  70. }
  71. for _, d := range diffs {
  72. if d != nil {
  73. if err := d.Validate(); err != nil {
  74. return err
  75. }
  76. }
  77. }
  78. for _, diff := range diffs {
  79. err := h.chain.HandleDiffLayer(diff, pid, fulfilled)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }