protocol.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/types"
  24. "github.com/ethereum/go-ethereum/event"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // Constants to match up protocol versions and messages
  28. const (
  29. eth62 = 62
  30. eth63 = 63
  31. )
  32. // ProtocolName is the official short name of the protocol used during capability negotiation.
  33. var ProtocolName = "eth"
  34. // ProtocolVersions are the upported versions of the eth protocol (first is primary).
  35. var ProtocolVersions = []uint{eth63, eth62}
  36. // ProtocolLengths are the number of implemented message corresponding to different protocol versions.
  37. var ProtocolLengths = []uint64{17, 8}
  38. const ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
  39. // eth protocol message codes
  40. const (
  41. // Protocol messages belonging to eth/62
  42. StatusMsg = 0x00
  43. NewBlockHashesMsg = 0x01
  44. TxMsg = 0x02
  45. GetBlockHeadersMsg = 0x03
  46. BlockHeadersMsg = 0x04
  47. GetBlockBodiesMsg = 0x05
  48. BlockBodiesMsg = 0x06
  49. NewBlockMsg = 0x07
  50. // Protocol messages belonging to eth/63
  51. GetNodeDataMsg = 0x0d
  52. NodeDataMsg = 0x0e
  53. GetReceiptsMsg = 0x0f
  54. ReceiptsMsg = 0x10
  55. )
  56. type errCode int
  57. const (
  58. ErrMsgTooLarge = iota
  59. ErrDecode
  60. ErrInvalidMsgCode
  61. ErrProtocolVersionMismatch
  62. ErrNetworkIdMismatch
  63. ErrGenesisBlockMismatch
  64. ErrNoStatusMsg
  65. ErrExtraStatusMsg
  66. ErrSuspendedPeer
  67. )
  68. func (e errCode) String() string {
  69. return errorToString[int(e)]
  70. }
  71. // XXX change once legacy code is out
  72. var errorToString = map[int]string{
  73. ErrMsgTooLarge: "Message too long",
  74. ErrDecode: "Invalid message",
  75. ErrInvalidMsgCode: "Invalid message code",
  76. ErrProtocolVersionMismatch: "Protocol version mismatch",
  77. ErrNetworkIdMismatch: "NetworkId mismatch",
  78. ErrGenesisBlockMismatch: "Genesis block mismatch",
  79. ErrNoStatusMsg: "No status message",
  80. ErrExtraStatusMsg: "Extra status message",
  81. ErrSuspendedPeer: "Suspended peer",
  82. }
  83. type txPool interface {
  84. // AddRemotes should add the given transactions to the pool.
  85. AddRemotes([]*types.Transaction) []error
  86. // Pending should return pending transactions.
  87. // The slice should be modifiable by the caller.
  88. Pending() (map[common.Address]types.Transactions, error)
  89. // SubscribeNewTxsEvent should return an event subscription of
  90. // NewTxsEvent and send events to the given channel.
  91. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  92. }
  93. // statusData is the network packet for the status message.
  94. type statusData struct {
  95. ProtocolVersion uint32
  96. NetworkId uint64
  97. TD *big.Int
  98. CurrentBlock common.Hash
  99. GenesisBlock common.Hash
  100. }
  101. // newBlockHashesData is the network packet for the block announcements.
  102. type newBlockHashesData []struct {
  103. Hash common.Hash // Hash of one particular block being announced
  104. Number uint64 // Number of one particular block being announced
  105. }
  106. // getBlockHeadersData represents a block header query.
  107. type getBlockHeadersData struct {
  108. Origin hashOrNumber // Block from which to retrieve headers
  109. Amount uint64 // Maximum number of headers to retrieve
  110. Skip uint64 // Blocks to skip between consecutive headers
  111. Reverse bool // Query direction (false = rising towards latest, true = falling towards genesis)
  112. }
  113. // hashOrNumber is a combined field for specifying an origin block.
  114. type hashOrNumber struct {
  115. Hash common.Hash // Block hash from which to retrieve headers (excludes Number)
  116. Number uint64 // Block hash from which to retrieve headers (excludes Hash)
  117. }
  118. // EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
  119. // two contained union fields.
  120. func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
  121. if hn.Hash == (common.Hash{}) {
  122. return rlp.Encode(w, hn.Number)
  123. }
  124. if hn.Number != 0 {
  125. return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
  126. }
  127. return rlp.Encode(w, hn.Hash)
  128. }
  129. // DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
  130. // into either a block hash or a block number.
  131. func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
  132. _, size, _ := s.Kind()
  133. origin, err := s.Raw()
  134. if err == nil {
  135. switch {
  136. case size == 32:
  137. err = rlp.DecodeBytes(origin, &hn.Hash)
  138. case size <= 8:
  139. err = rlp.DecodeBytes(origin, &hn.Number)
  140. default:
  141. err = fmt.Errorf("invalid input size %d for origin", size)
  142. }
  143. }
  144. return err
  145. }
  146. // newBlockData is the network packet for the block propagation message.
  147. type newBlockData struct {
  148. Block *types.Block
  149. TD *big.Int
  150. }
  151. // blockBody represents the data content of a single block.
  152. type blockBody struct {
  153. Transactions []*types.Transaction // Transactions contained within a block
  154. Uncles []*types.Header // Uncles contained within a block
  155. }
  156. // blockBodiesData is the network packet for block content distribution.
  157. type blockBodiesData []*blockBody