protocol.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. // Supported versions of the eth protocol (first is primary).
  32. var ProtocolVersions = []uint{eth63, eth62, eth61}
  33. // Number of implemented message corresponding to different protocol versions.
  34. var ProtocolLengths = []uint64{17, 8, 9}
  35. const (
  36. NetworkId = 1
  37. ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
  38. )
  39. // eth protocol message codes
  40. const (
  41. // Protocol messages belonging to eth/61
  42. StatusMsg = 0x00
  43. NewBlockHashesMsg = 0x01
  44. TxMsg = 0x02
  45. GetBlockHashesMsg = 0x03
  46. BlockHashesMsg = 0x04
  47. GetBlocksMsg = 0x05
  48. BlocksMsg = 0x06
  49. NewBlockMsg = 0x07
  50. GetBlockHashesFromNumberMsg = 0x08
  51. // Protocol messages belonging to eth/62 (new protocol from scratch)
  52. // StatusMsg = 0x00 (uncomment after eth/61 deprecation)
  53. // NewBlockHashesMsg = 0x01 (uncomment after eth/61 deprecation)
  54. // TxMsg = 0x02 (uncomment after eth/61 deprecation)
  55. GetBlockHeadersMsg = 0x03
  56. BlockHeadersMsg = 0x04
  57. GetBlockBodiesMsg = 0x05
  58. BlockBodiesMsg = 0x06
  59. // NewBlockMsg = 0x07 (uncomment after eth/61 deprecation)
  60. // Protocol messages belonging to eth/63
  61. GetNodeDataMsg = 0x0d
  62. NodeDataMsg = 0x0e
  63. GetReceiptsMsg = 0x0f
  64. ReceiptsMsg = 0x10
  65. )
  66. type errCode int
  67. const (
  68. ErrMsgTooLarge = iota
  69. ErrDecode
  70. ErrInvalidMsgCode
  71. ErrProtocolVersionMismatch
  72. ErrNetworkIdMismatch
  73. ErrGenesisBlockMismatch
  74. ErrNoStatusMsg
  75. ErrExtraStatusMsg
  76. ErrSuspendedPeer
  77. )
  78. func (e errCode) String() string {
  79. return errorToString[int(e)]
  80. }
  81. // XXX change once legacy code is out
  82. var errorToString = map[int]string{
  83. ErrMsgTooLarge: "Message too long",
  84. ErrDecode: "Invalid message",
  85. ErrInvalidMsgCode: "Invalid message code",
  86. ErrProtocolVersionMismatch: "Protocol version mismatch",
  87. ErrNetworkIdMismatch: "NetworkId mismatch",
  88. ErrGenesisBlockMismatch: "Genesis block mismatch",
  89. ErrNoStatusMsg: "No status message",
  90. ErrExtraStatusMsg: "Extra status message",
  91. ErrSuspendedPeer: "Suspended peer",
  92. }
  93. type txPool interface {
  94. // AddTransactions should add the given transactions to the pool.
  95. AddTransactions([]*types.Transaction)
  96. // GetTransactions should return pending transactions.
  97. // The slice should be modifiable by the caller.
  98. GetTransactions() types.Transactions
  99. }
  100. type chainManager interface {
  101. GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash)
  102. GetBlock(hash common.Hash) (block *types.Block)
  103. Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
  104. }
  105. // statusData is the network packet for the status message.
  106. type statusData struct {
  107. ProtocolVersion uint32
  108. NetworkId uint32
  109. TD *big.Int
  110. CurrentBlock common.Hash
  111. GenesisBlock common.Hash
  112. }
  113. // newBlockHashesData is the network packet for the block announcements.
  114. type newBlockHashesData []struct {
  115. Hash common.Hash // Hash of one particular block being announced
  116. Number uint64 // Number of one particular block being announced
  117. }
  118. // getBlockHashesData is the network packet for the hash based hash retrieval.
  119. type getBlockHashesData struct {
  120. Hash common.Hash
  121. Amount uint64
  122. }
  123. // getBlockHashesFromNumberData is the network packet for the number based hash
  124. // retrieval.
  125. type getBlockHashesFromNumberData struct {
  126. Number uint64
  127. Amount uint64
  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. // hashOrNumber is a combined field for specifying an origin block.
  137. type hashOrNumber struct {
  138. Hash common.Hash // Block hash from which to retrieve headers (excludes Number)
  139. Number uint64 // Block hash from which to retrieve headers (excludes Hash)
  140. }
  141. // EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
  142. // two contained union fields.
  143. func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
  144. if hn.Hash == (common.Hash{}) {
  145. return rlp.Encode(w, hn.Number)
  146. }
  147. if hn.Number != 0 {
  148. return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
  149. }
  150. return rlp.Encode(w, hn.Hash)
  151. }
  152. // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
  153. // into either a block hash or a block number.
  154. func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
  155. _, size, _ := s.Kind()
  156. origin, err := s.Raw()
  157. if err == nil {
  158. switch {
  159. case size == 32:
  160. err = rlp.DecodeBytes(origin, &hn.Hash)
  161. case size <= 8:
  162. err = rlp.DecodeBytes(origin, &hn.Number)
  163. default:
  164. err = fmt.Errorf("invalid input size %d for origin", size)
  165. }
  166. }
  167. return err
  168. }
  169. // newBlockData is the network packet for the block propagation message.
  170. type newBlockData struct {
  171. Block *types.Block
  172. TD *big.Int
  173. }
  174. // blockBody represents the data content of a single block.
  175. type blockBody struct {
  176. Transactions []*types.Transaction // Transactions contained within a block
  177. Uncles []*types.Header // Uncles contained within a block
  178. }
  179. // blockBodiesData is the network packet for block content distribution.
  180. type blockBodiesData []*blockBody
  181. // nodeDataData is the network response packet for a node data retrieval.
  182. type nodeDataData []struct {
  183. Value []byte
  184. }