protocol.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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"
  23. "github.com/ethereum/go-ethereum/core/forkid"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/event"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. // Constants to match up protocol versions and messages
  29. const (
  30. eth63 = 63
  31. eth64 = 64
  32. eth65 = 65
  33. )
  34. // protocolName is the official short name of the protocol used during capability negotiation.
  35. const protocolName = "eth"
  36. // ProtocolVersions are the supported versions of the eth protocol (first is primary).
  37. var ProtocolVersions = []uint{eth65, eth64, eth63}
  38. // protocolLengths are the number of implemented message corresponding to different protocol versions.
  39. var protocolLengths = map[uint]uint64{eth65: 17, eth64: 17, eth63: 17}
  40. const protocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
  41. // eth protocol message codes
  42. const (
  43. StatusMsg = 0x00
  44. NewBlockHashesMsg = 0x01
  45. TransactionMsg = 0x02
  46. GetBlockHeadersMsg = 0x03
  47. BlockHeadersMsg = 0x04
  48. GetBlockBodiesMsg = 0x05
  49. BlockBodiesMsg = 0x06
  50. NewBlockMsg = 0x07
  51. GetNodeDataMsg = 0x0d
  52. NodeDataMsg = 0x0e
  53. GetReceiptsMsg = 0x0f
  54. ReceiptsMsg = 0x10
  55. // New protocol message codes introduced in eth65
  56. //
  57. // Previously these message ids were used by some legacy and unsupported
  58. // eth protocols, reown them here.
  59. NewPooledTransactionHashesMsg = 0x08
  60. GetPooledTransactionsMsg = 0x09
  61. PooledTransactionsMsg = 0x0a
  62. )
  63. type errCode int
  64. const (
  65. ErrMsgTooLarge = iota
  66. ErrDecode
  67. ErrInvalidMsgCode
  68. ErrProtocolVersionMismatch
  69. ErrNetworkIDMismatch
  70. ErrGenesisMismatch
  71. ErrForkIDRejected
  72. ErrNoStatusMsg
  73. ErrExtraStatusMsg
  74. )
  75. func (e errCode) String() string {
  76. return errorToString[int(e)]
  77. }
  78. // XXX change once legacy code is out
  79. var errorToString = map[int]string{
  80. ErrMsgTooLarge: "Message too long",
  81. ErrDecode: "Invalid message",
  82. ErrInvalidMsgCode: "Invalid message code",
  83. ErrProtocolVersionMismatch: "Protocol version mismatch",
  84. ErrNetworkIDMismatch: "Network ID mismatch",
  85. ErrGenesisMismatch: "Genesis mismatch",
  86. ErrForkIDRejected: "Fork ID rejected",
  87. ErrNoStatusMsg: "No status message",
  88. ErrExtraStatusMsg: "Extra status message",
  89. }
  90. type txPool interface {
  91. // Has returns an indicator whether txpool has a transaction
  92. // cached with the given hash.
  93. Has(hash common.Hash) bool
  94. // Get retrieves the transaction from local txpool with given
  95. // tx hash.
  96. Get(hash common.Hash) *types.Transaction
  97. // AddRemotes should add the given transactions to the pool.
  98. AddRemotes([]*types.Transaction) []error
  99. // Pending should return pending transactions.
  100. // The slice should be modifiable by the caller.
  101. Pending() (map[common.Address]types.Transactions, error)
  102. // SubscribeNewTxsEvent should return an event subscription of
  103. // NewTxsEvent and send events to the given channel.
  104. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  105. }
  106. // statusData63 is the network packet for the status message for eth/63.
  107. type statusData63 struct {
  108. ProtocolVersion uint32
  109. NetworkId uint64
  110. TD *big.Int
  111. CurrentBlock common.Hash
  112. GenesisBlock common.Hash
  113. }
  114. // statusData is the network packet for the status message for eth/64 and later.
  115. type statusData struct {
  116. ProtocolVersion uint32
  117. NetworkID uint64
  118. TD *big.Int
  119. Head common.Hash
  120. Genesis common.Hash
  121. ForkID forkid.ID
  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. // getBlockHeadersData represents a block header query.
  129. type getBlockHeadersData struct {
  130. Origin hashOrNumber // Block from which to retrieve headers
  131. Amount uint64 // Maximum number of headers to retrieve
  132. Skip uint64 // Blocks to skip between consecutive headers
  133. Reverse bool // Query direction (false = rising towards latest, true = falling towards genesis)
  134. }
  135. // hashOrNumber is a combined field for specifying an origin block.
  136. type hashOrNumber struct {
  137. Hash common.Hash // Block hash from which to retrieve headers (excludes Number)
  138. Number uint64 // Block hash from which to retrieve headers (excludes Hash)
  139. }
  140. // EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
  141. // two contained union fields.
  142. func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
  143. if hn.Hash == (common.Hash{}) {
  144. return rlp.Encode(w, hn.Number)
  145. }
  146. if hn.Number != 0 {
  147. return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
  148. }
  149. return rlp.Encode(w, hn.Hash)
  150. }
  151. // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
  152. // into either a block hash or a block number.
  153. func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
  154. _, size, _ := s.Kind()
  155. origin, err := s.Raw()
  156. if err == nil {
  157. switch {
  158. case size == 32:
  159. err = rlp.DecodeBytes(origin, &hn.Hash)
  160. case size <= 8:
  161. err = rlp.DecodeBytes(origin, &hn.Number)
  162. default:
  163. err = fmt.Errorf("invalid input size %d for origin", size)
  164. }
  165. }
  166. return err
  167. }
  168. // newBlockData is the network packet for the block propagation message.
  169. type newBlockData struct {
  170. Block *types.Block
  171. TD *big.Int
  172. }
  173. // sanityCheck verifies that the values are reasonable, as a DoS protection
  174. func (request *newBlockData) sanityCheck() error {
  175. if err := request.Block.SanityCheck(); err != nil {
  176. return err
  177. }
  178. //TD at mainnet block #7753254 is 76 bits. If it becomes 100 million times
  179. // larger, it will still fit within 100 bits
  180. if tdlen := request.TD.BitLen(); tdlen > 100 {
  181. return fmt.Errorf("too large block TD: bitlen %d", tdlen)
  182. }
  183. return nil
  184. }
  185. // blockBody represents the data content of a single block.
  186. type blockBody struct {
  187. Transactions []*types.Transaction // Transactions contained within a block
  188. Uncles []*types.Header // Uncles contained within a block
  189. }
  190. // blockBodiesData is the network packet for block content distribution.
  191. type blockBodiesData []*blockBody