github.com/annchain/OG@v0.0.9/p2p/protocol.go (about)

     1  // Copyright 2014 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  //go:generate msgp
    17  package p2p
    18  
    19  import (
    20  	"fmt"
    21  	"github.com/annchain/OG/p2p/onode"
    22  )
    23  
    24  type MsgCodeType uint16
    25  
    26  // Protocol represents a P2P subprotocol implementation.
    27  //msgp:tuple Protocol
    28  type Protocol struct {
    29  	// Name should contain the official protocol name,
    30  	// often a three-letter word.
    31  	Name string
    32  
    33  	// Version should contain the version number of the protocol.
    34  	Version uint32
    35  
    36  	// Length should contain the number of message codes used
    37  	// by the protocol.
    38  	Length MsgCodeType
    39  
    40  	// Run is called in a new groutine when the protocol has been
    41  	// negotiated with a peer. It should read and write messages from
    42  	// rw. The Payload for each message must be fully consumed.
    43  	//
    44  	// The peer connection is closed when Start returns. It should return
    45  	// any protocol-level error (suc01h as an I/O error) that is
    46  	// encountered.
    47  	Run func(peer *Peer, rw MsgReadWriter) error
    48  
    49  	// NodeInfo is an optional helper method to retrieve protocol specific metadata
    50  	// about the host node.
    51  	NodeInfo func() interface{}
    52  
    53  	// PeerInfo is an optional helper method to retrieve protocol specific metadata
    54  	// about a certain peer in the network. If an info retrieval function is set,
    55  	// but returns nil, it is assumed that the protocol handshake is still running.
    56  	PeerInfo func(id onode.ID) interface{}
    57  
    58  	// Attributes contains protocol specific information for the node record.
    59  	//Attributes []enr.Entry
    60  }
    61  
    62  func (p Protocol) cap() Cap {
    63  	return Cap{p.Name, p.Version}
    64  }
    65  
    66  // Cap is the structure of a peer capability.
    67  //msgp:tuple Cap
    68  type Cap struct {
    69  	Name    string
    70  	Version uint32
    71  }
    72  
    73  // protoHandshake is the RLP structure of the protocol handshake.
    74  //msgp:tuple ProtoHandshake
    75  type ProtoHandshake struct {
    76  	Version    uint32 `msg:"version"`
    77  	Name       string `msg:"name"`
    78  	Caps       []Cap  `msg:"caps"`
    79  	ListenPort uint16 `msg:"listen_port"`
    80  	ID         []byte `msg:"id"` // secp256k1 public key
    81  
    82  	// Ignore additional fields (for forward compatibility).
    83  	//Rest [][]byte `rlp:"tail" msg:"tail"`
    84  }
    85  
    86  func (cap Cap) RlpData() interface{} {
    87  	return []interface{}{cap.Name, cap.Version}
    88  }
    89  
    90  func (cap Cap) String() string {
    91  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    92  }
    93  
    94  type CapsByNameAndVersion []Cap
    95  
    96  func (cs CapsByNameAndVersion) Len() int      { return len(cs) }
    97  func (cs CapsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
    98  func (cs CapsByNameAndVersion) Less(i, j int) bool {
    99  	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
   100  }
   101  
   102  func (CapsByNameAndVersion) ENRKey() string { return "cap" }