github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/les/protocol.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  
    25  	"github.com/ebakus/go-ebakus/common"
    26  	"github.com/ebakus/go-ebakus/crypto"
    27  	"github.com/ebakus/go-ebakus/p2p/enode"
    28  	"github.com/ebakus/go-ebakus/rlp"
    29  )
    30  
    31  // Constants to match up protocol versions and messages
    32  const (
    33  	lpv2 = 2
    34  	lpv3 = 3
    35  )
    36  
    37  // Supported versions of the les protocol (first is primary)
    38  var (
    39  	ClientProtocolVersions    = []uint{lpv2, lpv3}
    40  	ServerProtocolVersions    = []uint{lpv2, lpv3}
    41  	AdvertiseProtocolVersions = []uint{lpv2} // clients are searching for the first advertised protocol in the list
    42  )
    43  
    44  // Number of implemented message corresponding to different protocol versions.
    45  var ProtocolLengths = map[uint]uint64{lpv2: 22, lpv3: 24}
    46  
    47  const (
    48  	NetworkId          = 1
    49  	ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
    50  )
    51  
    52  // les protocol message codes
    53  const (
    54  	// Protocol messages inherited from LPV1
    55  	StatusMsg          = 0x00
    56  	AnnounceMsg        = 0x01
    57  	GetBlockHeadersMsg = 0x02
    58  	BlockHeadersMsg    = 0x03
    59  	GetBlockBodiesMsg  = 0x04
    60  	BlockBodiesMsg     = 0x05
    61  	GetReceiptsMsg     = 0x06
    62  	ReceiptsMsg        = 0x07
    63  	GetCodeMsg         = 0x0a
    64  	CodeMsg            = 0x0b
    65  	// Protocol messages introduced in LPV2
    66  	GetProofsV2Msg         = 0x0f
    67  	ProofsV2Msg            = 0x10
    68  	GetHelperTrieProofsMsg = 0x11
    69  	HelperTrieProofsMsg    = 0x12
    70  	SendTxV2Msg            = 0x13
    71  	GetTxStatusMsg         = 0x14
    72  	TxStatusMsg            = 0x15
    73  	// Protocol messages introduced in LPV3
    74  	StopMsg   = 0x16
    75  	ResumeMsg = 0x17
    76  )
    77  
    78  type requestInfo struct {
    79  	name     string
    80  	maxCount uint64
    81  }
    82  
    83  var requests = map[uint64]requestInfo{
    84  	GetBlockHeadersMsg:     {"GetBlockHeaders", MaxHeaderFetch},
    85  	GetBlockBodiesMsg:      {"GetBlockBodies", MaxBodyFetch},
    86  	GetReceiptsMsg:         {"GetReceipts", MaxReceiptFetch},
    87  	GetCodeMsg:             {"GetCode", MaxCodeFetch},
    88  	GetProofsV2Msg:         {"GetProofsV2", MaxProofsFetch},
    89  	GetHelperTrieProofsMsg: {"GetHelperTrieProofs", MaxHelperTrieProofsFetch},
    90  	SendTxV2Msg:            {"SendTxV2", MaxTxSend},
    91  	GetTxStatusMsg:         {"GetTxStatus", MaxTxStatus},
    92  }
    93  
    94  type errCode int
    95  
    96  const (
    97  	ErrMsgTooLarge = iota
    98  	ErrDecode
    99  	ErrInvalidMsgCode
   100  	ErrProtocolVersionMismatch
   101  	ErrNetworkIdMismatch
   102  	ErrGenesisBlockMismatch
   103  	ErrNoStatusMsg
   104  	ErrExtraStatusMsg
   105  	ErrSuspendedPeer
   106  	ErrUselessPeer
   107  	ErrRequestRejected
   108  	ErrUnexpectedResponse
   109  	ErrInvalidResponse
   110  	ErrTooManyTimeouts
   111  	ErrMissingKey
   112  )
   113  
   114  func (e errCode) String() string {
   115  	return errorToString[int(e)]
   116  }
   117  
   118  // XXX change once legacy code is out
   119  var errorToString = map[int]string{
   120  	ErrMsgTooLarge:             "Message too long",
   121  	ErrDecode:                  "Invalid message",
   122  	ErrInvalidMsgCode:          "Invalid message code",
   123  	ErrProtocolVersionMismatch: "Protocol version mismatch",
   124  	ErrNetworkIdMismatch:       "NetworkId mismatch",
   125  	ErrGenesisBlockMismatch:    "Genesis block mismatch",
   126  	ErrNoStatusMsg:             "No status message",
   127  	ErrExtraStatusMsg:          "Extra status message",
   128  	ErrSuspendedPeer:           "Suspended peer",
   129  	ErrRequestRejected:         "Request rejected",
   130  	ErrUnexpectedResponse:      "Unexpected response",
   131  	ErrInvalidResponse:         "Invalid response",
   132  	ErrTooManyTimeouts:         "Too many request timeouts",
   133  	ErrMissingKey:              "Key missing from list",
   134  }
   135  
   136  type announceBlock struct {
   137  	Hash   common.Hash // Hash of one particular block being announced
   138  	Number uint64      // Number of one particular block being announced
   139  }
   140  
   141  // announceData is the network packet for the block announcements.
   142  type announceData struct {
   143  	Hash       common.Hash // Hash of one particular block being announced
   144  	Number     uint64      // Number of one particular block being announced
   145  	ReorgDepth uint64
   146  	Update     keyValueList
   147  }
   148  
   149  // sanityCheck verifies that the values are reasonable, as a DoS protection
   150  func (a *announceData) sanityCheck() error {
   151  	return nil
   152  }
   153  
   154  // sign adds a signature to the block announcement by the given privKey
   155  func (a *announceData) sign(privKey *ecdsa.PrivateKey) {
   156  	rlp, _ := rlp.EncodeToBytes(announceBlock{a.Hash, a.Number})
   157  	sig, _ := crypto.Sign(crypto.Keccak256(rlp), privKey)
   158  	a.Update = a.Update.add("sign", sig)
   159  }
   160  
   161  // checkSignature verifies if the block announcement has a valid signature by the given pubKey
   162  func (a *announceData) checkSignature(id enode.ID, update keyValueMap) error {
   163  	var sig []byte
   164  	if err := update.get("sign", &sig); err != nil {
   165  		return err
   166  	}
   167  	rlp, _ := rlp.EncodeToBytes(announceBlock{a.Hash, a.Number})
   168  	recPubkey, err := crypto.SigToPub(crypto.Keccak256(rlp), sig)
   169  	if err != nil {
   170  		return err
   171  	}
   172  	if id == enode.PubkeyToIDV4(recPubkey) {
   173  		return nil
   174  	}
   175  	return errors.New("wrong signature")
   176  }
   177  
   178  type blockInfo struct {
   179  	Hash   common.Hash // Hash of one particular block being announced
   180  	Number uint64      // Number of one particular block being announced
   181  }
   182  
   183  // getBlockHeadersData represents a block header query.
   184  type getBlockHeadersData struct {
   185  	Origin  hashOrNumber // Block from which to retrieve headers
   186  	Amount  uint64       // Maximum number of headers to retrieve
   187  	Skip    uint64       // Blocks to skip between consecutive headers
   188  	Reverse bool         // Query direction (false = rising towards latest, true = falling towards genesis)
   189  }
   190  
   191  // hashOrNumber is a combined field for specifying an origin block.
   192  type hashOrNumber struct {
   193  	Hash   common.Hash // Block hash from which to retrieve headers (excludes Number)
   194  	Number uint64      // Block hash from which to retrieve headers (excludes Hash)
   195  }
   196  
   197  // EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
   198  // two contained union fields.
   199  func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
   200  	if hn.Hash == (common.Hash{}) {
   201  		return rlp.Encode(w, hn.Number)
   202  	}
   203  	if hn.Number != 0 {
   204  		return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
   205  	}
   206  	return rlp.Encode(w, hn.Hash)
   207  }
   208  
   209  // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
   210  // into either a block hash or a block number.
   211  func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
   212  	_, size, _ := s.Kind()
   213  	origin, err := s.Raw()
   214  	if err == nil {
   215  		switch {
   216  		case size == 32:
   217  			err = rlp.DecodeBytes(origin, &hn.Hash)
   218  		case size <= 8:
   219  			err = rlp.DecodeBytes(origin, &hn.Number)
   220  		default:
   221  			err = fmt.Errorf("invalid input size %d for origin", size)
   222  		}
   223  	}
   224  	return err
   225  }
   226  
   227  // CodeData is the network response packet for a node data retrieval.
   228  type CodeData []struct {
   229  	Value []byte
   230  }
   231  
   232  type proofsData [][]rlp.RawValue