github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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 17 package p2p 18 19 import ( 20 "fmt" 21 22 "github.com/ethereum/go-ethereum/p2p/enode" 23 "github.com/ethereum/go-ethereum/p2p/enr" 24 ) 25 26 // Protocol represents a P2P subprotocol implementation. 27 type Protocol struct { 28 // Name should contain the official protocol name, 29 // often a three-letter word. 30 Name string 31 32 // Version should contain the version number of the protocol. 33 Version uint 34 35 // Length should contain the number of message codes used 36 // by the protocol. 37 Length uint64 38 39 // Whether this should be the primary form of communication between nodes that support this protocol. 40 Primary bool 41 42 // Run is called in a new goroutine 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 // NodeInfo is an optional helper method to retrieve protocol specific metadata 52 // about the host node. 53 NodeInfo func() interface{} 54 55 // PeerInfo is an optional helper method to retrieve protocol specific metadata 56 // about a certain peer in the network. If an info retrieval function is set, 57 // but returns nil, it is assumed that the protocol handshake is still running. 58 PeerInfo func(id enode.ID) interface{} 59 60 // Attributes contains protocol specific information for the node record. 61 Attributes []enr.Entry 62 } 63 64 func (p Protocol) cap() Cap { 65 return Cap{p.Name, p.Version} 66 } 67 68 // Cap is the structure of a peer capability. 69 type Cap struct { 70 Name string 71 Version uint 72 } 73 74 func (cap Cap) String() string { 75 return fmt.Sprintf("%s/%d", cap.Name, cap.Version) 76 } 77 78 type capsByNameAndVersion []Cap 79 80 func (cs capsByNameAndVersion) Len() int { return len(cs) } 81 func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] } 82 func (cs capsByNameAndVersion) Less(i, j int) bool { 83 return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version) 84 } 85 86 func (capsByNameAndVersion) ENRKey() string { return "cap" }