protocol.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package eth
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. )
  22. // Supported versions of the eth protocol (first is primary).
  23. var ProtocolVersions = []uint{61, 60}
  24. // Number of implemented message corresponding to different protocol versions.
  25. var ProtocolLengths = []uint64{9, 8}
  26. const (
  27. NetworkId = 0
  28. ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
  29. )
  30. // eth protocol message codes
  31. const (
  32. StatusMsg = iota
  33. NewBlockHashesMsg
  34. TxMsg
  35. GetBlockHashesMsg
  36. BlockHashesMsg
  37. GetBlocksMsg
  38. BlocksMsg
  39. NewBlockMsg
  40. GetBlockHashesFromNumberMsg
  41. )
  42. type errCode int
  43. const (
  44. ErrMsgTooLarge = iota
  45. ErrDecode
  46. ErrInvalidMsgCode
  47. ErrProtocolVersionMismatch
  48. ErrNetworkIdMismatch
  49. ErrGenesisBlockMismatch
  50. ErrNoStatusMsg
  51. ErrExtraStatusMsg
  52. ErrSuspendedPeer
  53. )
  54. func (e errCode) String() string {
  55. return errorToString[int(e)]
  56. }
  57. // XXX change once legacy code is out
  58. var errorToString = map[int]string{
  59. ErrMsgTooLarge: "Message too long",
  60. ErrDecode: "Invalid message",
  61. ErrInvalidMsgCode: "Invalid message code",
  62. ErrProtocolVersionMismatch: "Protocol version mismatch",
  63. ErrNetworkIdMismatch: "NetworkId mismatch",
  64. ErrGenesisBlockMismatch: "Genesis block mismatch",
  65. ErrNoStatusMsg: "No status message",
  66. ErrExtraStatusMsg: "Extra status message",
  67. ErrSuspendedPeer: "Suspended peer",
  68. }
  69. type txPool interface {
  70. // AddTransactions should add the given transactions to the pool.
  71. AddTransactions([]*types.Transaction)
  72. // GetTransactions should return pending transactions.
  73. // The slice should be modifiable by the caller.
  74. GetTransactions() types.Transactions
  75. }
  76. type chainManager interface {
  77. GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash)
  78. GetBlock(hash common.Hash) (block *types.Block)
  79. Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
  80. }
  81. // statusData is the network packet for the status message.
  82. type statusData struct {
  83. ProtocolVersion uint32
  84. NetworkId uint32
  85. TD *big.Int
  86. CurrentBlock common.Hash
  87. GenesisBlock common.Hash
  88. }
  89. // getBlockHashesData is the network packet for the hash based block retrieval
  90. // message.
  91. type getBlockHashesData struct {
  92. Hash common.Hash
  93. Amount uint64
  94. }
  95. // getBlockHashesFromNumberData is the network packet for the number based block
  96. // retrieval message.
  97. type getBlockHashesFromNumberData struct {
  98. Number uint64
  99. Amount uint64
  100. }
  101. // newBlockData is the network packet for the block propagation message.
  102. type newBlockData struct {
  103. Block *types.Block
  104. TD *big.Int
  105. }