github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/backend/peer_handler.go (about) 1 // Copyright 2017 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 backend 18 19 import ( 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/p2p" 22 "github.com/ethereum/go-ethereum/p2p/enode" 23 ) 24 25 type validatorPeerHandler struct { 26 sb *Backend 27 } 28 29 // Returns whether this node should maintain validator connections 30 // Only proxies and non proxied validators need to connect maintain validator connections 31 func (vph *validatorPeerHandler) MaintainValConnections() bool { 32 return vph.sb.config.Proxy || (vph.sb.coreStarted && !vph.sb.config.Proxied) 33 } 34 35 func (vph *validatorPeerHandler) AddValidatorPeer(node *enode.Node, address common.Address) { 36 if !vph.MaintainValConnections() { 37 return 38 } 39 40 // Connect to the remote peer if it's part of the current epoch's valset and 41 // if this node is also part of the current epoch's valset 42 43 block := vph.sb.currentBlock() 44 valSet := vph.sb.getValidators(block.Number().Uint64(), block.Hash()) 45 if valSet.ContainsByAddress(address) && valSet.ContainsByAddress(vph.sb.ValidatorAddress()) { 46 vph.sb.p2pserver.AddPeer(node, p2p.ValidatorPurpose) 47 vph.sb.p2pserver.AddTrustedPeer(node, p2p.ValidatorPurpose) 48 } 49 } 50 51 func (vph *validatorPeerHandler) RemoveValidatorPeer(node *enode.Node) { 52 vph.sb.p2pserver.RemovePeer(node, p2p.ValidatorPurpose) 53 vph.sb.p2pserver.RemoveTrustedPeer(node, p2p.ValidatorPurpose) 54 } 55 56 func (vph *validatorPeerHandler) ReplaceValidatorPeers(newNodes []*enode.Node) { 57 nodeIDSet := make(map[enode.ID]bool) 58 for _, node := range newNodes { 59 nodeIDSet[node.ID()] = true 60 } 61 62 // Remove old Validator Peers 63 for existingPeerID, existingPeer := range vph.sb.broadcaster.FindPeers(nodeIDSet, p2p.ValidatorPurpose) { 64 if !nodeIDSet[existingPeerID] { 65 vph.RemoveValidatorPeer(existingPeer.Node()) 66 } 67 } 68 69 if vph.MaintainValConnections() { 70 // Add new Validator Peers (adds all the nodes in newNodes. Note that add is noOp on already existent ones) 71 for _, newNode := range newNodes { 72 vph.sb.p2pserver.AddPeer(newNode, p2p.ValidatorPurpose) 73 vph.sb.p2pserver.AddTrustedPeer(newNode, p2p.ValidatorPurpose) 74 } 75 } 76 } 77 78 func (vph *validatorPeerHandler) ClearValidatorPeers() { 79 for _, peer := range vph.sb.broadcaster.FindPeers(nil, p2p.ValidatorPurpose) { 80 vph.sb.p2pserver.RemovePeer(peer.Node(), p2p.ValidatorPurpose) 81 vph.sb.p2pserver.RemoveTrustedPeer(peer.Node(), p2p.ValidatorPurpose) 82 } 83 }