protocol.go 5.4 KB

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