github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/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 gossip
    18  
    19  import (
    20  	"github.com/unicornultrafoundation/go-u2u/eth/protocols/snap"
    21  	"github.com/unicornultrafoundation/go-u2u/p2p/enode"
    22  )
    23  
    24  // snapHandler implements the snap.Backend interface to handle the various network
    25  // packets that are sent as replies or broadcasts.
    26  type snapHandler handler
    27  
    28  func (h *snapHandler) Chain() snap.BlockChain {
    29  	return h.chain
    30  }
    31  
    32  // RunPeer is invoked when a peer joins on the `snap` protocol.
    33  func (h *snapHandler) RunPeer(peer *snap.Peer, hand snap.Handler) error {
    34  	return (*handler)(h).runSnapExtension(peer, hand)
    35  }
    36  
    37  // PeerInfo retrieves all known `snap` information about a peer.
    38  func (h *snapHandler) PeerInfo(id enode.ID) interface{} {
    39  	if p := h.peers.Peer(id.String()); p != nil {
    40  		if p.snapExt != nil {
    41  			return p.snapExt.info()
    42  		}
    43  	}
    44  	return nil
    45  }
    46  
    47  // Handle is invoked from a peer's message handler when it receives a new remote
    48  // message that the handler couldn't consume and serve itself.
    49  func (h *snapHandler) Handle(peer *snap.Peer, packet snap.Packet) error {
    50  	return h.snapLeecher.DeliverSnapPacket(peer, packet)
    51  }
    52  
    53  // runSnapExtension registers a `snap` peer into the joint eth/snap peerset and
    54  // starts handling inbound messages. As `snap` is only a satellite protocol to
    55  // `eth`, all subsystem registrations and lifecycle management will be done by
    56  // the main `eth` handler to prevent strange races.
    57  func (h *handler) runSnapExtension(peer *snap.Peer, handler snap.Handler) error {
    58  	h.peerWG.Add(1)
    59  	defer h.peerWG.Done()
    60  	if err := h.peers.RegisterSnapExtension(peer); err != nil {
    61  		logger := peer.Log().Error
    62  		if err == errSnapWithoutU2U {
    63  			logger = peer.Log().Trace
    64  		}
    65  		logger("Snapshot extension registration failed", "err", err)
    66  		return err
    67  	}
    68  	return handler(peer)
    69  }