protocol.go 5.4 KB

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