MCPcopy Create free account
hub / github.com/CPChain/chain / addPeer

Method addPeer

protocols/cpc/handler.go:250–285  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

248// addPeer is the callback invoked to manage the life cycle of cpchain peer.
249// when this function terminates, the peer is disconnected.
250func (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
287func (pm *ProtocolManager) handlePeer(p *p2p.Peer, rw p2p.MsgReadWriter, version uint) error {
288 var (

Callers 1

handlePeerMethod · 0.95

Calls 10

GenesisMethod · 0.80
HandshakeMethod · 0.80
DebugMethod · 0.80
LenMethod · 0.65
InfoMethod · 0.65
CurrentHeaderMethod · 0.65
HashMethod · 0.65
InitMethod · 0.45
RegisterMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected