protocol.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. package eth
  17. import (
  18. "fmt"
  19. "io"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Constants to match up protocol versions and messages
  26. const (
  27. eth61 = 61
  28. eth62 = 62
  29. eth63 = 63
  30. )
  31. // Official short name of the protocol used during capability negotiation.
  32. var ProtocolName = "eth"
  33. // Supported versions of the eth protocol (first is primary).
  34. var ProtocolVersions = []uint{eth63, eth62, eth61}
  35. // Number of implemented message corresponding to different protocol versions.
  36. var ProtocolLengths = []uint64{17, 8, 9}
  37. const (
  38. NetworkId = 1
  39. ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
  40. )
  41. // eth protocol message codes
  42. const (
  43. // Protocol messages belonging to eth/61
  44. StatusMsg = 0x00
  45. NewBlockHashesMsg = 0x01
  46. TxMsg = 0x02
  47. GetBlockHashesMsg = 0x03
  48. BlockHashesMsg = 0x04
  49. GetBlocksMsg = 0x05
  50. BlocksMsg = 0x06
  51. NewBlockMsg = 0x07
  52. GetBlockHashesFromNumberMsg = 0x08
  53. // Protocol messages belonging to eth/62 (new protocol from scratch)
  54. // StatusMsg = 0x00 (uncomment after eth/61 deprecation)
  55. // NewBlockHashesMsg = 0x01 (uncomment after eth/61 deprecation)
  56. // TxMsg = 0x02 (uncomment after eth/61 deprecation)
  57. GetBlockHeadersMsg = 0x03
  58. BlockHeadersMsg = 0x04
  59. GetBlockBodiesMsg = 0x05
  60. BlockBodiesMsg = 0x06
  61. // NewBlockMsg = 0x07 (uncomment after eth/61 deprecation)
  62. // Protocol messages belonging to eth/63
  63. GetNodeDataMsg = 0x0d
  64. NodeDataMsg = 0x0e
  65. GetReceiptsMsg = 0x0f
  66. ReceiptsMsg = 0x10
  67. )
  68. type errCode int
  69. const (
  70. ErrMsgTooLarge = iota
  71. ErrDecode
  72. ErrInvalidMsgCode
  73. ErrProtocolVersionMismatch
  74. ErrNetworkIdMismatch
  75. ErrGenesisBlockMismatch
  76. ErrNoStatusMsg
  77. ErrExtraStatusMsg
  78. ErrSuspendedPeer
  79. )
  80. func (e errCode) String() string {
  81. return errorToString[int(e)]
  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. type txPool interface {
  96. // AddTransactions should add the given transactions to the pool.
  97. AddTransactions([]*types.Transaction)
  98. // GetTransactions should return pending transactions.
  99. // The slice should be modifiable by the caller.
  100. GetTransactions() types.Transactions
  101. }
  102. type chainManager interface {
  103. GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash)
  104. GetBlock(hash common.Hash) (block *types.Block)
  105. Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
  106. }
  107. // statusData is the network packet for the status message.
  108. type statusData struct {
  109. ProtocolVersion uint32
  110. NetworkId uint32
  111. TD *big.Int
  112. CurrentBlock common.Hash
  113. GenesisBlock common.Hash
  114. }
  115. // newBlockHashesData is the network packet for the block announcements.
  116. type newBlockHashesData []struct {
  117. Hash common.Hash // Hash of one particular block being announced
  118. Number uint64 // Number of one particular block being announced
  119. }
  120. // getBlockHashesData is the network packet for the hash based hash retrieval.
  121. type getBlockHashesData struct {
  122. Hash common.Hash
  123. Amount uint64
  124. }
  125. // getBlockHashesFromNumberData is the network packet for the number based hash
  126. // retrieval.
  127. type getBlockHashesFromNumberData struct {
  128. Number uint64
  129. Amount uint64
  130. }
  131. // getBlockHeadersData represents a block header query.
  132. type getBlockHeadersData struct {
  133. Origin hashOrNumber // Block from which to retrieve headers
  134. Amount uint64 // Maximum number of headers to retrieve
  135. Skip uint64 // Blocks to skip between consecutive headers
  136. Reverse bool // Query direction (false = rising towards latest, true = falling towards genesis)
  137. }
  138. // hashOrNumber is a combined field for specifying an origin block.
  139. type hashOrNumber struct {
  140. Hash common.Hash // Block hash from which to retrieve headers (excludes Number)
  141. Number uint64 // Block hash from which to retrieve headers (excludes Hash)
  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. // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
  155. // into either a block hash or a block number.
  156. func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
  157. _, size, _ := s.Kind()
  158. origin, err := s.Raw()
  159. if err == nil {
  160. switch {
  161. case size == 32:
  162. err = rlp.DecodeBytes(origin, &hn.Hash)
  163. case size <= 8:
  164. err = rlp.DecodeBytes(origin, &hn.Number)
  165. default:
  166. err = fmt.Errorf("invalid input size %d for origin", size)
  167. }
  168. }
  169. return err
  170. }
  171. // newBlockData is the network packet for the block propagation message.
  172. type newBlockData struct {
  173. Block *types.Block
  174. TD *big.Int
  175. }
  176. // blockBody represents the data content of a single block.
  177. type blockBody struct {
  178. Transactions []*types.Transaction // Transactions contained within a block
  179. Uncles []*types.Header // Uncles contained within a block
  180. }
  181. // blockBodiesData is the network packet for block content distribution.
  182. type blockBodiesData []*blockBody
  183. // nodeDataData is the network response packet for a node data retrieval.
  184. type nodeDataData []struct {
  185. Value []byte
  186. }