handler_diff.go 2.8 KB

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