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