github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/p2p" 30 "github.com/ethereum/go-ethereum/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 // Send writes an RLP-encoded message with the given code. 197 // data should encode as an RLP list. 198 func (p *peer) Send(msgcode uint64, data interface{}) error { 199 return p2p.Send(p.rw, msgcode, data) 200 } 201 202 // SendTransactions sends transactions to the peer and includes the hashes 203 // in its transaction hash set for future reference. 204 func (p *peer) SendTransactions(txs types.Transactions) error { 205 for _, tx := range txs { 206 p.knownTxs.Add(tx.Hash()) 207 } 208 return p2p.Send(p.rw, TxMsg, txs) 209 } 210 211 // AsyncSendTransactions queues list of transactions propagation to a remote 212 // peer. If the peer's broadcast queue is full, the event is silently dropped. 213 func (p *peer) AsyncSendTransactions(txs []*types.Transaction) { 214 select { 215 case p.queuedTxs <- txs: 216 for _, tx := range txs { 217 p.knownTxs.Add(tx.Hash()) 218 } 219 default: 220 p.Log().Debug("Dropping transaction propagation", "count", len(txs)) 221 } 222 } 223 224 // SendNewBlockHashes announces the availability of a number of blocks through 225 // a hash notification. 226 func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error { 227 for _, hash := range hashes { 228 p.knownBlocks.Add(hash) 229 } 230 request := make(newBlockHashesData, len(hashes)) 231 for i := 0; i < len(hashes); i++ { 232 request[i].Hash = hashes[i] 233 request[i].Number = numbers[i] 234 } 235 return p2p.Send(p.rw, NewBlockHashesMsg, request) 236 } 237 238 // AsyncSendNewBlockHash queues the availability of a block for propagation to a 239 // remote peer. If the peer's broadcast queue is full, the event is silently 240 // dropped. 241 func (p *peer) AsyncSendNewBlockHash(block *types.Block) { 242 select { 243 case p.queuedAnns <- block: 244 p.knownBlocks.Add(block.Hash()) 245 default: 246 p.Log().Debug("Dropping block announcement", "number", block.NumberU64(), "hash", block.Hash()) 247 } 248 } 249 250 // SendNewBlock propagates an entire block to a remote peer. 251 func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error { 252 p.knownBlocks.Add(block.Hash()) 253 return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td}) 254 } 255 256 // AsyncSendNewBlock queues an entire block for propagation to a remote peer. If 257 // the peer's broadcast queue is full, the event is silently dropped. 258 func (p *peer) AsyncSendNewBlock(block *types.Block, td *big.Int) { 259 select { 260 case p.queuedProps <- &propEvent{block: block, td: td}: 261 p.knownBlocks.Add(block.Hash()) 262 default: 263 p.Log().Debug("Dropping block propagation", "number", block.NumberU64(), "hash", block.Hash()) 264 } 265 } 266 267 // SendBlockHeaders sends a batch of block headers to the remote peer. 268 func (p *peer) SendBlockHeaders(headers []*types.Header) error { 269 return p2p.Send(p.rw, BlockHeadersMsg, headers) 270 } 271 272 // SendBlockBodies sends a batch of block contents to the remote peer. 273 func (p *peer) SendBlockBodies(bodies []*types.Body) error { 274 return p2p.Send(p.rw, BlockBodiesMsg, blockBodiesData(bodies)) 275 } 276 277 // SendBlockBodiesRLP sends a batch of block contents to the remote peer from 278 // an already RLP encoded format. 279 func (p *peer) SendBlockBodiesRLP(bodies []rlp.RawValue) error { 280 return p2p.Send(p.rw, BlockBodiesMsg, bodies) 281 } 282 283 // SendNodeDataRLP sends a batch of arbitrary internal data, corresponding to the 284 // hashes requested. 285 func (p *peer) SendNodeData(data [][]byte) error { 286 return p2p.Send(p.rw, NodeDataMsg, data) 287 } 288 289 // SendReceiptsRLP sends a batch of transaction receipts, corresponding to the 290 // ones requested from an already RLP encoded format. 291 func (p *peer) SendReceiptsRLP(receipts []rlp.RawValue) error { 292 return p2p.Send(p.rw, ReceiptsMsg, receipts) 293 } 294 295 // RequestOneHeader is a wrapper around the header query functions to fetch a 296 // single header. It is used solely by the fetcher. 297 func (p *peer) RequestOneHeader(hash common.Hash) error { 298 p.Log().Debug("Fetching single header", "hash", hash) 299 return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: hash}, Amount: uint64(1), Skip: uint64(0), Reverse: false}) 300 } 301 302 // RequestHeadersByHash fetches a batch of blocks' headers corresponding to the 303 // specified header query, based on the hash of an origin block. 304 func (p *peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { 305 p.Log().Debug("Fetching batch of headers", "count", amount, "fromhash", origin, "skip", skip, "reverse", reverse) 306 return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 307 } 308 309 // RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the 310 // specified header query, based on the number of an origin block. 311 func (p *peer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { 312 p.Log().Debug("Fetching batch of headers", "count", amount, "fromnum", origin, "skip", skip, "reverse", reverse) 313 return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Number: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 314 } 315 316 // RequestBodies fetches a batch of blocks' bodies corresponding to the hashes 317 // specified. 318 func (p *peer) RequestBodies(hashes []common.Hash) error { 319 p.Log().Debug("Fetching batch of block bodies", "count", len(hashes)) 320 return p2p.Send(p.rw, GetBlockBodiesMsg, hashes) 321 } 322 323 // RequestNodeData fetches a batch of arbitrary data from a node's known state 324 // data, corresponding to the specified hashes. 325 func (p *peer) RequestNodeData(hashes []common.Hash) error { 326 p.Log().Debug("Fetching batch of state data", "count", len(hashes)) 327 return p2p.Send(p.rw, GetNodeDataMsg, hashes) 328 } 329 330 // RequestReceipts fetches a batch of transaction receipts from a remote node. 331 func (p *peer) RequestReceipts(hashes []common.Hash) error { 332 p.Log().Debug("Fetching batch of receipts", "count", len(hashes)) 333 return p2p.Send(p.rw, GetReceiptsMsg, hashes) 334 } 335 336 // Handshake executes the eth protocol handshake, negotiating version number, 337 // network IDs, difficulties, head and genesis blocks. 338 func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash) error { 339 // Send out own handshake in a new thread 340 errc := make(chan error, 2) 341 var status statusData // safe to read after two values have been received from errc 342 343 go func() { 344 errc <- p2p.Send(p.rw, StatusMsg, &statusData{ 345 ProtocolVersion: uint32(p.version), 346 NetworkId: network, 347 TD: td, 348 CurrentBlock: head, 349 GenesisBlock: genesis, 350 }) 351 }() 352 go func() { 353 errc <- p.readStatus(network, &status, genesis) 354 }() 355 timeout := time.NewTimer(handshakeTimeout) 356 defer timeout.Stop() 357 for i := 0; i < 2; i++ { 358 select { 359 case err := <-errc: 360 if err != nil { 361 return err 362 } 363 case <-timeout.C: 364 return p2p.DiscReadTimeout 365 } 366 } 367 p.td, p.head = status.TD, status.CurrentBlock 368 return nil 369 } 370 371 func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash) (err error) { 372 msg, err := p.rw.ReadMsg() 373 if err != nil { 374 return err 375 } 376 if msg.Code != StatusMsg { 377 return errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg) 378 } 379 if msg.Size > ProtocolMaxMsgSize { 380 return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) 381 } 382 // Decode the handshake and make sure everything matches 383 if err := msg.Decode(&status); err != nil { 384 return errResp(ErrDecode, "msg %v: %v", msg, err) 385 } 386 if status.GenesisBlock != genesis { 387 return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock[:8], genesis[:8]) 388 } 389 if status.NetworkId != network { 390 return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, network) 391 } 392 if int(status.ProtocolVersion) != p.version { 393 return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version) 394 } 395 return nil 396 } 397 398 // String implements fmt.Stringer. 399 func (p *peer) String() string { 400 return fmt.Sprintf("Peer %s [%s]", p.id, 401 fmt.Sprintf("eth/%2d", p.version), 402 ) 403 } 404 405 // peerSet represents the collection of active peers currently participating in 406 // the Ethereum sub-protocol. 407 type peerSet struct { 408 peers map[string]*peer 409 lock sync.RWMutex 410 closed bool 411 } 412 413 // newPeerSet creates a new peer set to track the active participants. 414 func newPeerSet() *peerSet { 415 return &peerSet{ 416 peers: make(map[string]*peer), 417 } 418 } 419 420 // Register injects a new peer into the working set, or returns an error if the 421 // peer is already known. If a new peer it registered, its broadcast loop is also 422 // started. 423 func (ps *peerSet) Register(p *peer) error { 424 ps.lock.Lock() 425 defer ps.lock.Unlock() 426 427 if ps.closed { 428 return errClosed 429 } 430 if _, ok := ps.peers[p.id]; ok { 431 return errAlreadyRegistered 432 } 433 ps.peers[p.id] = p 434 go p.broadcast() 435 436 return nil 437 } 438 439 // Unregister removes a remote peer from the active set, disabling any further 440 // actions to/from that particular entity. 441 func (ps *peerSet) Unregister(id string) error { 442 ps.lock.Lock() 443 defer ps.lock.Unlock() 444 445 p, ok := ps.peers[id] 446 if !ok { 447 return errNotRegistered 448 } 449 delete(ps.peers, id) 450 p.close() 451 452 return nil 453 } 454 455 // Peers returns all registered peers 456 func (ps *peerSet) Peers() map[string]*peer { 457 ps.lock.RLock() 458 defer ps.lock.RUnlock() 459 460 set := make(map[string]*peer) 461 for id, p := range ps.peers { 462 set[id] = p 463 } 464 return set 465 } 466 467 // Peer retrieves the registered peer with the given id. 468 func (ps *peerSet) Peer(id string) *peer { 469 ps.lock.RLock() 470 defer ps.lock.RUnlock() 471 472 return ps.peers[id] 473 } 474 475 // Len returns if the current number of peers in the set. 476 func (ps *peerSet) Len() int { 477 ps.lock.RLock() 478 defer ps.lock.RUnlock() 479 480 return len(ps.peers) 481 } 482 483 // PeersWithoutBlock retrieves a list of peers that do not have a given block in 484 // their set of known hashes. 485 func (ps *peerSet) PeersWithoutBlock(hash common.Hash) []*peer { 486 ps.lock.RLock() 487 defer ps.lock.RUnlock() 488 489 list := make([]*peer, 0, len(ps.peers)) 490 for _, p := range ps.peers { 491 if !p.knownBlocks.Contains(hash) { 492 list = append(list, p) 493 } 494 } 495 return list 496 } 497 498 // PeersWithoutTx retrieves a list of peers that do not have a given transaction 499 // in their set of known hashes. 500 func (ps *peerSet) PeersWithoutTx(hash common.Hash) []*peer { 501 ps.lock.RLock() 502 defer ps.lock.RUnlock() 503 504 list := make([]*peer, 0, len(ps.peers)) 505 for _, p := range ps.peers { 506 if !p.knownTxs.Contains(hash) { 507 list = append(list, p) 508 } 509 } 510 return list 511 } 512 513 // BestPeer retrieves the known peer with the currently highest total difficulty. 514 func (ps *peerSet) BestPeer() *peer { 515 ps.lock.RLock() 516 defer ps.lock.RUnlock() 517 518 var ( 519 bestPeer *peer 520 bestTd *big.Int 521 ) 522 for _, p := range ps.peers { 523 if _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 { 524 bestPeer, bestTd = p, td 525 } 526 } 527 return bestPeer 528 } 529 530 // Close disconnects all peers. 531 // No new peers can be registered after Close has returned. 532 func (ps *peerSet) Close() { 533 ps.lock.Lock() 534 defer ps.lock.Unlock() 535 536 for _, p := range ps.peers { 537 p.Disconnect(p2p.DiscQuitting) 538 } 539 ps.closed = true 540 }