addPeer is the callback invoked to manage the life cycle of cpchain peer. when this function terminates, the peer is disconnected.
(p *peer, isMinerOrValidator bool)
| 248 | // addPeer is the callback invoked to manage the life cycle of cpchain peer. |
| 249 | // when this function terminates, the peer is disconnected. |
| 250 | func (pm *ProtocolManager) addPeer(p *peer, isMinerOrValidator bool) (bool, error) { |
| 251 | // ignore maxPeers if this is a trusted or a static peer |
| 252 | if pm.peers.Len() >= pm.maxPeers && !(p.Peer.Info().Network.Trusted || p.Peer.Info().Network.Static) { |
| 253 | return false, p2p.DiscTooManyPeers |
| 254 | } |
| 255 | |
| 256 | // Execute the cpchain handshake |
| 257 | var ( |
| 258 | genesis = pm.blockchain.Genesis() |
| 259 | head = pm.blockchain.CurrentHeader() |
| 260 | hash = head.Hash() |
| 261 | height = head.Number |
| 262 | ) |
| 263 | |
| 264 | // Do normal handshake |
| 265 | remoteIsMiner, err := p.Handshake(pm.networkID, height, hash, genesis.Hash(), isMinerOrValidator) |
| 266 | |
| 267 | if err != nil { |
| 268 | log.Debug("Cpchain handshake failed", "err", err) |
| 269 | return false, err |
| 270 | } |
| 271 | |
| 272 | if rw, ok := p.rw.(*meteredMsgReadWriter); ok { |
| 273 | rw.Init(p.version) |
| 274 | } |
| 275 | |
| 276 | // add the peer to peerset |
| 277 | if err := pm.peers.Register(p); err != nil { |
| 278 | log.Debug("register new peer ") |
| 279 | return false, err |
| 280 | } |
| 281 | |
| 282 | log.Debug("Cpchain peer connected", "name", p.Name()) |
| 283 | |
| 284 | return remoteIsMiner, nil |
| 285 | } |
| 286 | |
| 287 | func (pm *ProtocolManager) handlePeer(p *p2p.Peer, rw p2p.MsgReadWriter, version uint) error { |
| 288 | var ( |