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