github.com/klaytn/klaytn@v1.12.1/networks/p2p/protocol.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from p2p/protocol.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package p2p
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/klaytn/klaytn/networks/p2p/discover"
    27  )
    28  
    29  // Protocol represents a P2P subprotocol implementation.
    30  type Protocol struct {
    31  	// Name should contain the official protocol name,
    32  	// often a three-letter word.
    33  	Name string
    34  
    35  	// Version should contain the version number of the protocol.
    36  	Version uint
    37  
    38  	// Length should contain the number of message codes used
    39  	// by the protocol.
    40  	Length uint64
    41  
    42  	// Run is called in a new groutine when the protocol has been
    43  	// negotiated with a peer. It should read and write messages from
    44  	// rw. The Payload for each message must be fully consumed.
    45  	//
    46  	// The peer connection is closed when Start returns. It should return
    47  	// any protocol-level error (such as an I/O error) that is
    48  	// encountered.
    49  	Run func(peer *Peer, rw MsgReadWriter) error
    50  
    51  	// RunWithRWs is called in a new groutine when the protocol has been
    52  	// negotiated with a peer. It should read and write messages from
    53  	// rws. The Payload for each message must be fully consumed.
    54  	//
    55  	// The peer connection is closed when Start returns. It should return
    56  	// any protocol-level error (such as an I/O error) that is
    57  	// encountered.
    58  	RunWithRWs func(peer *Peer, rws []MsgReadWriter) error
    59  
    60  	// NodeInfo is an optional helper method to retrieve protocol specific metadata
    61  	// about the host node.
    62  	NodeInfo func() interface{}
    63  
    64  	// PeerInfo is an optional helper method to retrieve protocol specific metadata
    65  	// about a certain peer in the network. If an info retrieval function is set,
    66  	// but returns nil, it is assumed that the protocol handshake is still running.
    67  	PeerInfo func(id discover.NodeID) interface{}
    68  }
    69  
    70  func (p Protocol) cap() Cap {
    71  	return Cap{p.Name, p.Version}
    72  }
    73  
    74  // Cap is the structure of a peer capability.
    75  type Cap struct {
    76  	Name    string
    77  	Version uint
    78  }
    79  
    80  func (cap Cap) RlpData() interface{} {
    81  	return []interface{}{cap.Name, cap.Version}
    82  }
    83  
    84  func (cap Cap) String() string {
    85  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    86  }
    87  
    88  type capsByNameAndVersion []Cap
    89  
    90  func (cs capsByNameAndVersion) Len() int      { return len(cs) }
    91  func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
    92  func (cs capsByNameAndVersion) Less(i, j int) bool {
    93  	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
    94  }