github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/cmd/devp2p/internal/ethtest/protocol.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 package ethtest 17 18 import ( 19 "github.com/ethereum/go-ethereum/p2p" 20 "github.com/ethereum/go-ethereum/rlp" 21 ) 22 23 // Unexported devp2p message codes from p2p/peer.go. 24 const ( 25 handshakeMsg = 0x00 26 discMsg = 0x01 27 pingMsg = 0x02 28 pongMsg = 0x03 29 ) 30 31 // Unexported devp2p protocol lengths from p2p package. 32 const ( 33 baseProtoLen = 16 34 ethProtoLen = 17 35 snapProtoLen = 8 36 ) 37 38 // Unexported handshake structure from p2p/peer.go. 39 type protoHandshake struct { 40 Version uint64 41 Name string 42 Caps []p2p.Cap 43 ListenPort uint64 44 ID []byte 45 Rest []rlp.RawValue `rlp:"tail"` 46 } 47 48 type Hello = protoHandshake 49 50 // Proto is an enum representing devp2p protocol types. 51 type Proto int 52 53 const ( 54 baseProto Proto = iota 55 ethProto 56 snapProto 57 ) 58 59 // getProto returns the protocol a certain message code is associated with 60 // (assuming the negotiated capabilities are exactly {eth,snap}) 61 func getProto(code uint64) Proto { 62 switch { 63 case code < baseProtoLen: 64 return baseProto 65 case code < baseProtoLen+ethProtoLen: 66 return ethProto 67 case code < baseProtoLen+ethProtoLen+snapProtoLen: 68 return snapProto 69 default: 70 panic("unhandled msg code beyond last protocol") 71 } 72 } 73 74 // protoOffset will return the offset at which the specified protocol's messages 75 // begin. 76 func protoOffset(proto Proto) uint64 { 77 switch proto { 78 case baseProto: 79 return 0 80 case ethProto: 81 return baseProtoLen 82 case snapProto: 83 return baseProtoLen + ethProtoLen 84 default: 85 panic("unhandled protocol") 86 } 87 }