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