github.com/ylsGit/go-ethereum@v1.6.5/eth/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 eth
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/rlp"
    27  )
    28  
    29  // Constants to match up protocol versions and messages
    30  const (
    31  	eth62 = 62
    32  	eth63 = 63
    33  )
    34  
    35  // Official short name of the protocol used during capability negotiation.
    36  var ProtocolName = "eth"
    37  
    38  // Supported versions of the eth protocol (first is primary).
    39  var ProtocolVersions = []uint{eth63, eth62}
    40  
    41  // Number of implemented message corresponding to different protocol versions.
    42  var ProtocolLengths = []uint64{17, 8}
    43  
    44  const ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
    45  
    46  // eth protocol message codes
    47  const (
    48  	// Protocol messages belonging to eth/62
    49  	StatusMsg          = 0x00
    50  	NewBlockHashesMsg  = 0x01
    51  	TxMsg              = 0x02
    52  	GetBlockHeadersMsg = 0x03
    53  	BlockHeadersMsg    = 0x04
    54  	GetBlockBodiesMsg  = 0x05
    55  	BlockBodiesMsg     = 0x06
    56  	NewBlockMsg        = 0x07
    57  
    58  	// Protocol messages belonging to eth/63
    59  	GetNodeDataMsg = 0x0d
    60  	NodeDataMsg    = 0x0e
    61  	GetReceiptsMsg = 0x0f
    62  	ReceiptsMsg    = 0x10
    63  )
    64  
    65  type errCode int
    66  
    67  const (
    68  	ErrMsgTooLarge = iota
    69  	ErrDecode
    70  	ErrInvalidMsgCode
    71  	ErrProtocolVersionMismatch
    72  	ErrNetworkIdMismatch
    73  	ErrGenesisBlockMismatch
    74  	ErrNoStatusMsg
    75  	ErrExtraStatusMsg
    76  	ErrSuspendedPeer
    77  )
    78  
    79  func (e errCode) String() string {
    80  	return errorToString[int(e)]
    81  }
    82  
    83  // XXX change once legacy code is out
    84  var errorToString = map[int]string{
    85  	ErrMsgTooLarge:             "Message too long",
    86  	ErrDecode:                  "Invalid message",
    87  	ErrInvalidMsgCode:          "Invalid message code",
    88  	ErrProtocolVersionMismatch: "Protocol version mismatch",
    89  	ErrNetworkIdMismatch:       "NetworkId mismatch",
    90  	ErrGenesisBlockMismatch:    "Genesis block mismatch",
    91  	ErrNoStatusMsg:             "No status message",
    92  	ErrExtraStatusMsg:          "Extra status message",
    93  	ErrSuspendedPeer:           "Suspended peer",
    94  }
    95  
    96  type txPool interface {
    97  	// AddBatch should add the given transactions to the pool.
    98  	AddBatch([]*types.Transaction) error
    99  
   100  	// Pending should return pending transactions.
   101  	// The slice should be modifiable by the caller.
   102  	Pending() (map[common.Address]types.Transactions, error)
   103  }
   104  
   105  // statusData is the network packet for the status message.
   106  type statusData struct {
   107  	ProtocolVersion uint32
   108  	NetworkId       uint64
   109  	TD              *big.Int
   110  	CurrentBlock    common.Hash
   111  	GenesisBlock    common.Hash
   112  }
   113  
   114  // newBlockHashesData is the network packet for the block announcements.
   115  type newBlockHashesData []struct {
   116  	Hash   common.Hash // Hash of one particular block being announced
   117  	Number uint64      // Number of one particular block being announced
   118  }
   119  
   120  // getBlockHeadersData represents a block header query.
   121  type getBlockHeadersData struct {
   122  	Origin  hashOrNumber // Block from which to retrieve headers
   123  	Amount  uint64       // Maximum number of headers to retrieve
   124  	Skip    uint64       // Blocks to skip between consecutive headers
   125  	Reverse bool         // Query direction (false = rising towards latest, true = falling towards genesis)
   126  }
   127  
   128  // hashOrNumber is a combined field for specifying an origin block.
   129  type hashOrNumber struct {
   130  	Hash   common.Hash // Block hash from which to retrieve headers (excludes Number)
   131  	Number uint64      // Block hash from which to retrieve headers (excludes Hash)
   132  }
   133  
   134  // EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
   135  // two contained union fields.
   136  func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
   137  	if hn.Hash == (common.Hash{}) {
   138  		return rlp.Encode(w, hn.Number)
   139  	}
   140  	if hn.Number != 0 {
   141  		return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
   142  	}
   143  	return rlp.Encode(w, hn.Hash)
   144  }
   145  
   146  // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
   147  // into either a block hash or a block number.
   148  func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
   149  	_, size, _ := s.Kind()
   150  	origin, err := s.Raw()
   151  	if err == nil {
   152  		switch {
   153  		case size == 32:
   154  			err = rlp.DecodeBytes(origin, &hn.Hash)
   155  		case size <= 8:
   156  			err = rlp.DecodeBytes(origin, &hn.Number)
   157  		default:
   158  			err = fmt.Errorf("invalid input size %d for origin", size)
   159  		}
   160  	}
   161  	return err
   162  }
   163  
   164  // newBlockData is the network packet for the block propagation message.
   165  type newBlockData struct {
   166  	Block *types.Block
   167  	TD    *big.Int
   168  }
   169  
   170  // blockBody represents the data content of a single block.
   171  type blockBody struct {
   172  	Transactions []*types.Transaction // Transactions contained within a block
   173  	Uncles       []*types.Header      // Uncles contained within a block
   174  }
   175  
   176  // blockBodiesData is the network packet for block content distribution.
   177  type blockBodiesData []*blockBody