github.com/ethereum/go-ethereum@v1.16.1/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  
    17  package ethtest
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/p2p"
    21  	"github.com/ethereum/go-ethereum/rlp"
    22  )
    23  
    24  // Unexported devp2p message codes from p2p/peer.go.
    25  const (
    26  	handshakeMsg = 0x00
    27  	discMsg      = 0x01
    28  	pingMsg      = 0x02
    29  	pongMsg      = 0x03
    30  )
    31  
    32  // Unexported devp2p protocol lengths from p2p package.
    33  const (
    34  	baseProtoLen = 16
    35  	ethProtoLen  = 18
    36  	snapProtoLen = 8
    37  )
    38  
    39  // Unexported handshake structure from p2p/peer.go.
    40  type protoHandshake struct {
    41  	Version    uint64
    42  	Name       string
    43  	Caps       []p2p.Cap
    44  	ListenPort uint64
    45  	ID         []byte
    46  	Rest       []rlp.RawValue `rlp:"tail"`
    47  }
    48  
    49  type Hello = protoHandshake
    50  
    51  // Proto is an enum representing devp2p protocol types.
    52  type Proto int
    53  
    54  const (
    55  	baseProto Proto = iota
    56  	ethProto
    57  	snapProto
    58  )
    59  
    60  // getProto returns the protocol a certain message code is associated with
    61  // (assuming the negotiated capabilities are exactly {eth,snap})
    62  func getProto(code uint64) Proto {
    63  	switch {
    64  	case code < baseProtoLen:
    65  		return baseProto
    66  	case code < baseProtoLen+ethProtoLen:
    67  		return ethProto
    68  	case code < baseProtoLen+ethProtoLen+snapProtoLen:
    69  		return snapProto
    70  	default:
    71  		panic("unhandled msg code beyond last protocol")
    72  	}
    73  }
    74  
    75  // protoOffset will return the offset at which the specified protocol's messages
    76  // begin.
    77  func protoOffset(proto Proto) uint64 {
    78  	switch proto {
    79  	case baseProto:
    80  		return 0
    81  	case ethProto:
    82  		return baseProtoLen
    83  	case snapProto:
    84  		return baseProtoLen + ethProtoLen
    85  	default:
    86  		panic("unhandled protocol")
    87  	}
    88  }