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