github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/vnt/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 vnt
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"math/big"
    23  
    24  	"github.com/vntchain/go-vnt/common"
    25  	"github.com/vntchain/go-vnt/core"
    26  	"github.com/vntchain/go-vnt/core/types"
    27  	"github.com/vntchain/go-vnt/event"
    28  	"github.com/vntchain/go-vnt/rlp"
    29  )
    30  
    31  // Constants to match up protocol versions and messages
    32  const (
    33  	vnt62 = 62
    34  	vnt63 = 63
    35  )
    36  
    37  // ProtocolName is the official short name of the protocol used during capability negotiation.
    38  var ProtocolName = "vnt"
    39  
    40  // ProtocolVersions are the upported versions of the vnt protocol (first is primary).
    41  var ProtocolVersions = []uint{vnt63, vnt62}
    42  
    43  // ProtocolLengths are the number of implemented message corresponding to different protocol versions.
    44  var ProtocolLengths = []uint64{20, 8}
    45  
    46  const ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
    47  
    48  // vnt protocol message codes
    49  const (
    50  	// Protocol messages belonging to vnt/62
    51  	StatusMsg          = 0x00
    52  	NewBlockHashesMsg  = 0x01
    53  	TxMsg              = 0x02
    54  	GetBlockHeadersMsg = 0x03
    55  	BlockHeadersMsg    = 0x04
    56  	GetBlockBodiesMsg  = 0x05
    57  	BlockBodiesMsg     = 0x06
    58  	NewBlockMsg        = 0x07
    59  
    60  	// Protocol messages belonging to vnt/63
    61  	GetNodeDataMsg   = 0x0d
    62  	NodeDataMsg      = 0x0e
    63  	GetReceiptsMsg   = 0x0f
    64  	ReceiptsMsg      = 0x10
    65  	BftPreprepareMsg = 0x11
    66  	BftPrepareMsg    = 0x12
    67  	BftCommitMsg     = 0x13
    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  	// SubscribeNewTxsEvent should return an event subscription of
   110  	// NewTxsEvent and send events to the given channel.
   111  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) 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  type newBlockHashData struct {
   124  	Hash       common.Hash // Hash of one particular block being announced
   125  	Number     uint64      // Number of one particular block being announced
   126  	ParentHash common.Hash // Hash of parent block
   127  	ParentTD   *big.Int    // Total difficulty of parent block
   128  }
   129  
   130  // newBlockHashesData is the network packet for the block announcements.
   131  type newBlockHashesData []newBlockHashData
   132  
   133  // getBlockHeadersData represents a block header query.
   134  type getBlockHeadersData struct {
   135  	Origin  hashOrNumber // Block from which to retrieve headers
   136  	Amount  uint64       // Maximum number of headers to retrieve
   137  	Skip    uint64       // Blocks to skip between consecutive headers
   138  	Reverse bool         // Query direction (false = rising towards latest, true = falling towards genesis)
   139  }
   140  
   141  // hashOrNumber is a combined field for specifying an origin block.
   142  type hashOrNumber struct {
   143  	Hash   common.Hash // Block hash from which to retrieve headers (excludes Number)
   144  	Number uint64      // Block hash from which to retrieve headers (excludes Hash)
   145  }
   146  
   147  // EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
   148  // two contained union fields.
   149  func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
   150  	if hn.Hash == (common.Hash{}) {
   151  		return rlp.Encode(w, hn.Number)
   152  	}
   153  	if hn.Number != 0 {
   154  		return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
   155  	}
   156  	return rlp.Encode(w, hn.Hash)
   157  }
   158  
   159  // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
   160  // into either a block hash or a block number.
   161  func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
   162  	_, size, _ := s.Kind()
   163  	origin, err := s.Raw()
   164  	if err == nil {
   165  		switch {
   166  		case size == 32:
   167  			err = rlp.DecodeBytes(origin, &hn.Hash)
   168  		case size <= 8:
   169  			err = rlp.DecodeBytes(origin, &hn.Number)
   170  		default:
   171  			err = fmt.Errorf("invalid input size %d for origin", size)
   172  		}
   173  	}
   174  	return err
   175  }
   176  
   177  // newBlockData is the network packet for the block propagation message.
   178  type newBlockData struct {
   179  	Block *types.Block
   180  	TD    *big.Int
   181  }
   182  
   183  // blockBody represents the data content of a single block.
   184  type blockBody struct {
   185  	Transactions []*types.Transaction // Transactions contained within a block
   186  }
   187  
   188  // blockBodiesData is the network packet for block content distribution.
   189  type blockBodiesData []*blockBody