github.com/luckypickle/go-ethereum-vet@v1.14.2/eth/peer.go (about) 1 // Copyright 2015 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 "errors" 21 "fmt" 22 "math/big" 23 "sync" 24 "time" 25 26 mapset "github.com/deckarep/golang-set" 27 "github.com/luckypickle/go-ethereum-vet/common" 28 "github.com/luckypickle/go-ethereum-vet/core/types" 29 "github.com/luckypickle/go-ethereum-vet/p2p" 30 "github.com/luckypickle/go-ethereum-vet/rlp" 31 ) 32 33 var ( 34 errClosed = errors.New("peer set is closed") 35 errAlreadyRegistered = errors.New("peer is already registered") 36 errNotRegistered = errors.New("peer is not registered") 37 ) 38 39 const ( 40 maxKnownTxs = 32768 // Maximum transactions hashes to keep in the known list (prevent DOS) 41 maxKnownBlocks = 1024 // Maximum block hashes to keep in the known list (prevent DOS) 42 43 // maxQueuedTxs is the maximum number of transaction lists to queue up before 44 // dropping broadcasts. This is a sensitive number as a transaction list might 45 // contain a single transaction, or thousands. 46 maxQueuedTxs = 128 47 48 // maxQueuedProps is the maximum number of block propagations to queue up before 49 // dropping broadcasts. There's not much point in queueing stale blocks, so a few 50 // that might cover uncles should be enough. 51 maxQueuedProps = 4 52 53 // maxQueuedAnns is the maximum number of block announcements to queue up before 54 // dropping broadcasts. Similarly to block propagations, there's no point to queue 55 // above some healthy uncle limit, so use that. 56 maxQueuedAnns = 4 57 58 handshakeTimeout = 5 * time.Second 59 ) 60 61 // PeerInfo represents a short summary of the Ethereum sub-protocol metadata known 62 // about a connected peer. 63 type PeerInfo struct { 64 Version int `json:"version"` // Ethereum protocol version negotiated 65 Difficulty *big.Int `json:"difficulty"` // Total difficulty of the peer's blockchain 66 Head string `json:"head"` // SHA3 hash of the peer's best owned block 67 } 68 69 // propEvent is a block propagation, waiting for its turn in the broadcast queue. 70 type propEvent struct { 71 block *types.Block 72 td *big.Int 73 } 74 75 type peer struct { 76 id string 77 78 *p2p.Peer 79 rw p2p.MsgReadWriter 80 81 version int // Protocol version negotiated 82 forkDrop *time.Timer // Timed connection dropper if forks aren't validated in time 83 84 head common.Hash 85 td *big.Int 86 lock sync.RWMutex 87 88 knownTxs mapset.Set // Set of transaction hashes known to be known by this peer 89 knownBlocks mapset.Set // Set of block hashes known to be known by this peer 90 queuedTxs chan []*types.Transaction // Queue of transactions to broadcast to the peer 91 queuedProps chan *propEvent // Queue of blocks to broadcast to the peer 92 queuedAnns chan *types.Block // Queue of blocks to announce to the peer 93 term chan struct{} // Termination channel to stop the broadcaster 94 } 95 96 func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { 97 return &peer{ 98 Peer: p, 99 rw: rw, 100 version: version, 101 id: fmt.Sprintf("%x", p.ID().Bytes()[:8]), 102 knownTxs: mapset.NewSet(), 103 knownBlocks: mapset.NewSet(), 104 queuedTxs: make(chan []*types.Transaction, maxQueuedTxs), 105 queuedProps: make(chan *propEvent, maxQueuedProps), 106 queuedAnns: make(chan *types.Block, maxQueuedAnns), 107 term: make(chan struct{}), 108 } 109 } 110 111 // broadcast is a write loop that multiplexes block propagations, announcements 112 // and transaction broadcasts into the remote peer. The goal is to have an async 113 // writer that does not lock up node internals. 114 func (p *peer) broadcast() { 115 for { 116 select { 117 case txs := <-p.queuedTxs: 118 if err := p.SendTransactions(txs); err != nil { 119 return 120 } 121 p.Log().Trace("Broadcast transactions", "count", len(txs)) 122 123 case prop := <-p.queuedProps: 124 if err := p.SendNewBlock(prop.block, prop.td); err != nil { 125 return 126 } 127 p.Log().Trace("Propagated block", "number", prop.block.Number(), "hash", prop.block.Hash(), "td", prop.td) 128 129 case block := <-p.queuedAnns: 130 if err := p.SendNewBlockHashes([]common.Hash{block.Hash()}, []uint64{block.NumberU64()}); err != nil { 131 return 132 } 133 p.Log().Trace("Announced block", "number", block.Number(), "hash", block.Hash()) 134 135 case <-p.term: 136 return 137 } 138 } 139 } 140 141 // close signals the broadcast goroutine to terminate. 142 func (p *peer) close() { 143 close(p.term) 144 } 145 146 // Info gathers and returns a collection of metadata known about a peer. 147 func (p *peer) Info() *PeerInfo { 148 hash, td := p.Head() 149 150 return &PeerInfo{ 151 Version: p.version, 152 Difficulty: td, 153 Head: hash.Hex(), 154 } 155 } 156 157 // Head retrieves a copy of the current head hash and total difficulty of the 158 // peer. 159 func (p *peer) Head() (hash common.Hash, td *big.Int) { 160 p.lock.RLock() 161 defer p.lock.RUnlock() 162 163 copy(hash[:], p.head[:]) 164 return hash, new(big.Int).Set(p.td) 165 } 166 167 // SetHead updates the head hash and total difficulty of the peer. 168 func (p *peer) SetHead(hash common.Hash, td *big.Int) { 169 p.lock.Lock() 170 defer p.lock.Unlock() 171 172 copy(p.head[:], hash[:]) 173 p.td.Set(td) 174 } 175 176 // MarkBlock marks a block as known for the peer, ensuring that the block will 177 // never be propagated to this particular peer. 178 func (p *peer) MarkBlock(hash common.Hash) { 179 // If we reached the memory allowance, drop a previously known block hash 180 for p.knownBlocks.Cardinality() >= maxKnownBlocks { 181 p.knownBlocks.Pop() 182 } 183 p.knownBlocks.Add(hash) 184 } 185 186 // MarkTransaction marks a transaction as known for the peer, ensuring that it 187 // will never be propagated to this particular peer. 188 func (p *peer) MarkTransaction(hash common.Hash) { 189 // If we reached the memory allowance, drop a previously known transaction hash 190 for p.knownTxs.Cardinality() >= maxKnownTxs { 191 p.knownTxs.Pop() 192 } 193 p.knownTxs.Add(hash) 194 } 195 196 // SendTransactions sends transactions to the peer and includes the hashes 197 // in its transaction hash set for future reference. 198 func (p *peer) SendTransactions(txs types.Transactions) error { 199 for _, tx := range txs { 200 p.knownTxs.Add(tx.Hash()) 201 } 202 return p2p.Send(p.rw, TxMsg, txs) 203 } 204 205 // AsyncSendTransactions queues list of transactions propagation to a remote 206 // peer. If the peer's broadcast queue is full, the event is silently dropped. 207 func (p *peer) AsyncSendTransactions(txs []*types.Transaction) { 208 select { 209 case p.queuedTxs <- txs: 210 for _, tx := range txs { 211 p.knownTxs.Add(tx.Hash()) 212 } 213 default: 214 p.Log().Debug("Dropping transaction propagation", "count", len(txs)) 215 } 216 } 217 218 // SendNewBlockHashes announces the availability of a number of blocks through 219 // a hash notification. 220 func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error { 221 for _, hash := range hashes { 222 p.knownBlocks.Add(hash) 223 } 224 request := make(newBlockHashesData, len(hashes)) 225 for i := 0; i < len(hashes); i++ { 226 request[i].Hash = hashes[i] 227 request[i].Number = numbers[i] 228 } 229 return p2p.Send(p.rw, NewBlockHashesMsg, request) 230 } 231 232 // AsyncSendNewBlockHash queues the availability of a block for propagation to a 233 // remote peer. If the peer's broadcast queue is full, the event is silently 234 // dropped. 235 func (p *peer) AsyncSendNewBlockHash(block *types.Block) { 236 select { 237 case p.queuedAnns <- block: 238 p.knownBlocks.Add(block.Hash()) 239 default: 240 p.Log().Debug("Dropping block announcement", "number", block.NumberU64(), "hash", block.Hash()) 241 } 242 } 243 244 // SendNewBlock propagates an entire block to a remote peer. 245 func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error { 246 p.knownBlocks.Add(block.Hash()) 247 return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td}) 248 } 249 250 // AsyncSendNewBlock queues an entire block for propagation to a remote peer. If 251 // the peer's broadcast queue is full, the event is silently dropped. 252 func (p *peer) AsyncSendNewBlock(block *types.Block, td *big.Int) { 253 select { 254 case p.queuedProps <- &propEvent{block: block, td: td}: 255 p.knownBlocks.Add(block.Hash()) 256 default: 257 p.Log().Debug("Dropping block propagation", "number", block.NumberU64(), "hash", block.Hash()) 258 } 259 } 260 261 // SendBlockHeaders sends a batch of block headers to the remote peer. 262 func (p *peer) SendBlockHeaders(headers []*types.Header) error { 263 return p2p.Send(p.rw, BlockHeadersMsg, headers) 264 } 265 266 // SendBlockBodies sends a batch of block contents to the remote peer. 267 func (p *peer) SendBlockBodies(bodies []*blockBody) error { 268 return p2p.Send(p.rw, BlockBodiesMsg, blockBodiesData(bodies)) 269 } 270 271 // SendBlockBodiesRLP sends a batch of block contents to the remote peer from 272 // an already RLP encoded format. 273 func (p *peer) SendBlockBodiesRLP(bodies []rlp.RawValue) error { 274 return p2p.Send(p.rw, BlockBodiesMsg, bodies) 275 } 276 277 // SendNodeDataRLP sends a batch of arbitrary internal data, corresponding to the 278 // hashes requested. 279 func (p *peer) SendNodeData(data [][]byte) error { 280 return p2p.Send(p.rw, NodeDataMsg, data) 281 } 282 283 // SendReceiptsRLP sends a batch of transaction receipts, corresponding to the 284 // ones requested from an already RLP encoded format. 285 func (p *peer) SendReceiptsRLP(receipts []rlp.RawValue) error { 286 return p2p.Send(p.rw, ReceiptsMsg, receipts) 287 } 288 289 // RequestOneHeader is a wrapper around the header query functions to fetch a 290 // single header. It is used solely by the fetcher. 291 func (p *peer) RequestOneHeader(hash common.Hash) error { 292 p.Log().Debug("Fetching single header", "hash", hash) 293 return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: hash}, Amount: uint64(1), Skip: uint64(0), Reverse: false}) 294 } 295 296 // RequestHeadersByHash fetches a batch of blocks' headers corresponding to the 297 // specified header query, based on the hash of an origin block. 298 func (p *peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { 299 p.Log().Debug("Fetching batch of headers", "count", amount, "fromhash", origin, "skip", skip, "reverse", reverse) 300 return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 301 } 302 303 // RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the 304 // specified header query, based on the number of an origin block. 305 func (p *peer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { 306 p.Log().Debug("Fetching batch of headers", "count", amount, "fromnum", origin, "skip", skip, "reverse", reverse) 307 return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Number: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 308 } 309 310 // RequestBodies fetches a batch of blocks' bodies corresponding to the hashes 311 // specified. 312 func (p *peer) RequestBodies(hashes []common.Hash) error { 313 p.Log().Debug("Fetching batch of block bodies", "count", len(hashes)) 314 return p2p.Send(p.rw, GetBlockBodiesMsg, hashes) 315 } 316 317 // RequestNodeData fetches a batch of arbitrary data from a node's known state 318 // data, corresponding to the specified hashes. 319 func (p *peer) RequestNodeData(hashes []common.Hash) error { 320 p.Log().Debug("Fetching batch of state data", "count", len(hashes)) 321 return p2p.Send(p.rw, GetNodeDataMsg, hashes) 322 } 323 324 // RequestReceipts fetches a batch of transaction receipts from a remote node. 325 func (p *peer) RequestReceipts(hashes []common.Hash) error { 326 p.Log().Debug("Fetching batch of receipts", "count", len(hashes)) 327 return p2p.Send(p.rw, GetReceiptsMsg, hashes) 328 } 329 330 // Handshake executes the eth protocol handshake, negotiating version number, 331 // network IDs, difficulties, head and genesis blocks. 332 func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash) error { 333 // Send out own handshake in a new thread 334 errc := make(chan error, 2) 335 var status statusData // safe to read after two values have been received from errc 336 337 go func() { 338 errc <- p2p.Send(p.rw, StatusMsg, &statusData{ 339 ProtocolVersion: uint32(p.version), 340 NetworkId: network, 341 TD: td, 342 CurrentBlock: head, 343 GenesisBlock: genesis, 344 }) 345 }() 346 go func() { 347 errc <- p.readStatus(network, &status, genesis) 348 }() 349 timeout := time.NewTimer(handshakeTimeout) 350 defer timeout.Stop() 351 for i := 0; i < 2; i++ { 352 select { 353 case err := <-errc: 354 if err != nil { 355 return err 356 } 357 case <-timeout.C: 358 return p2p.DiscReadTimeout 359 } 360 } 361 p.td, p.head = status.TD, status.CurrentBlock 362 return nil 363 } 364 365 func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash) (err error) { 366 msg, err := p.rw.ReadMsg() 367 if err != nil { 368 return err 369 } 370 if msg.Code != StatusMsg { 371 return errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg) 372 } 373 if msg.Size > ProtocolMaxMsgSize { 374 return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) 375 } 376 // Decode the handshake and make sure everything matches 377 if err := msg.Decode(&status); err != nil { 378 return errResp(ErrDecode, "msg %v: %v", msg, err) 379 } 380 if status.GenesisBlock != genesis { 381 return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock[:8], genesis[:8]) 382 } 383 if status.NetworkId != network { 384 return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, network) 385 } 386 if int(status.ProtocolVersion) != p.version { 387 return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version) 388 } 389 return nil 390 } 391 392 // String implements fmt.Stringer. 393 func (p *peer) String() string { 394 return fmt.Sprintf("Peer %s [%s]", p.id, 395 fmt.Sprintf("eth/%2d", p.version), 396 ) 397 } 398 399 // peerSet represents the collection of active peers currently participating in 400 // the Ethereum sub-protocol. 401 type peerSet struct { 402 peers map[string]*peer 403 lock sync.RWMutex 404 closed bool 405 } 406 407 // newPeerSet creates a new peer set to track the active participants. 408 func newPeerSet() *peerSet { 409 return &peerSet{ 410 peers: make(map[string]*peer), 411 } 412 } 413 414 // Register injects a new peer into the working set, or returns an error if the 415 // peer is already known. If a new peer it registered, its broadcast loop is also 416 // started. 417 func (ps *peerSet) Register(p *peer) error { 418 ps.lock.Lock() 419 defer ps.lock.Unlock() 420 421 if ps.closed { 422 return errClosed 423 } 424 if _, ok := ps.peers[p.id]; ok { 425 return errAlreadyRegistered 426 } 427 ps.peers[p.id] = p 428 go p.broadcast() 429 430 return nil 431 } 432 433 // Unregister removes a remote peer from the active set, disabling any further 434 // actions to/from that particular entity. 435 func (ps *peerSet) Unregister(id string) error { 436 ps.lock.Lock() 437 defer ps.lock.Unlock() 438 439 p, ok := ps.peers[id] 440 if !ok { 441 return errNotRegistered 442 } 443 delete(ps.peers, id) 444 p.close() 445 446 return nil 447 } 448 449 // Peer retrieves the registered peer with the given id. 450 func (ps *peerSet) Peer(id string) *peer { 451 ps.lock.RLock() 452 defer ps.lock.RUnlock() 453 454 return ps.peers[id] 455 } 456 457 // Len returns if the current number of peers in the set. 458 func (ps *peerSet) Len() int { 459 ps.lock.RLock() 460 defer ps.lock.RUnlock() 461 462 return len(ps.peers) 463 } 464 465 // PeersWithoutBlock retrieves a list of peers that do not have a given block in 466 // their set of known hashes. 467 func (ps *peerSet) PeersWithoutBlock(hash common.Hash) []*peer { 468 ps.lock.RLock() 469 defer ps.lock.RUnlock() 470 471 list := make([]*peer, 0, len(ps.peers)) 472 for _, p := range ps.peers { 473 if !p.knownBlocks.Contains(hash) { 474 list = append(list, p) 475 } 476 } 477 return list 478 } 479 480 // PeersWithoutTx retrieves a list of peers that do not have a given transaction 481 // in their set of known hashes. 482 func (ps *peerSet) PeersWithoutTx(hash common.Hash) []*peer { 483 ps.lock.RLock() 484 defer ps.lock.RUnlock() 485 486 list := make([]*peer, 0, len(ps.peers)) 487 for _, p := range ps.peers { 488 if !p.knownTxs.Contains(hash) { 489 list = append(list, p) 490 } 491 } 492 return list 493 } 494 495 // BestPeer retrieves the known peer with the currently highest total difficulty. 496 func (ps *peerSet) BestPeer() *peer { 497 ps.lock.RLock() 498 defer ps.lock.RUnlock() 499 500 var ( 501 bestPeer *peer 502 bestTd *big.Int 503 ) 504 for _, p := range ps.peers { 505 if _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 { 506 bestPeer, bestTd = p, td 507 } 508 } 509 return bestPeer 510 } 511 512 // Close disconnects all peers. 513 // No new peers can be registered after Close has returned. 514 func (ps *peerSet) Close() { 515 ps.lock.Lock() 516 defer ps.lock.Unlock() 517 518 for _, p := range ps.peers { 519 p.Disconnect(p2p.DiscQuitting) 520 } 521 ps.closed = true 522 }