github.com/bcnmy/go-ethereum@v1.10.27/eth/handler_snap.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  	"github.com/ethereum/go-ethereum/core"
    21  	"github.com/ethereum/go-ethereum/eth/protocols/snap"
    22  	"github.com/ethereum/go-ethereum/p2p/enode"
    23  )
    24  
    25  // snapHandler implements the snap.Backend interface to handle the various network
    26  // packets that are sent as replies or broadcasts.
    27  type snapHandler handler
    28  
    29  func (h *snapHandler) Chain() *core.BlockChain { return h.chain }
    30  
    31  // RunPeer is invoked when a peer joins on the `snap` protocol.
    32  func (h *snapHandler) RunPeer(peer *snap.Peer, hand snap.Handler) error {
    33  	return (*handler)(h).runSnapExtension(peer, hand)
    34  }
    35  
    36  // PeerInfo retrieves all known `snap` information about a peer.
    37  func (h *snapHandler) PeerInfo(id enode.ID) interface{} {
    38  	if p := h.peers.peer(id.String()); p != nil {
    39  		if p.snapExt != nil {
    40  			return p.snapExt.info()
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  // Handle is invoked from a peer's message handler when it receives a new remote
    47  // message that the handler couldn't consume and serve itself.
    48  func (h *snapHandler) Handle(peer *snap.Peer, packet snap.Packet) error {
    49  	return h.downloader.DeliverSnapPacket(peer, packet)
    50  }