MCPcopy
hub / github.com/canopy-network/canopy / Add

Method Add

p2p/set.go:54–81  ·  view source on GitHub ↗

Add() introduces a peer to the set

(p *Peer)

Source from the content-addressed store, hash-verified

52
53// Add() introduces a peer to the set
54func (ps *PeerSet) Add(p *Peer) (err lib.ErrorI) {
55 // check if peer is already added
56 pubKey := lib.BytesToString(p.Address.PublicKey)
57 if _, found := ps.m[pubKey]; found {
58 return ErrPeerAlreadyExists(pubKey)
59 }
60 // ensure peer is not self
61 if bytes.Equal(p.Address.PublicKey, ps.publicKey) {
62 return nil
63 }
64 // if not trusted and not must connect, check inbound/outbound limits
65 if !p.IsTrusted && !p.IsMustConnect {
66 if err = ps.validateRegularPeerLimits(p); err != nil {
67 return
68 }
69 }
70 // increment counts
71 if p.IsOutbound {
72 ps.outbound++
73 } else {
74 ps.inbound++
75 }
76 // set the peer
77 ps.set(p)
78 // update metrics
79 ps.updateMetrics()
80 return nil
81}
82
83// validateRegularPeerLimits checks if adding a regular peer exceeds configured limits
84func (ps *PeerSet) validateRegularPeerLimits(p *Peer) lib.ErrorI {

Callers 15

waitForTxInclusionFunction · 0.45
CheckMempoolMethod · 0.45
CheckMempoolMethod · 0.45
ListenForBlockMethod · 0.45
FuzzKeyDecodeEncodeFunction · 0.45
CommitParallelMethod · 0.45
IndexBlockMethod · 0.45
TestGetRandomFunction · 0.45
TestGetAllFunction · 0.45
TestAddRemoveHasFunction · 0.45
TestAddFailedDialAttemptFunction · 0.45

Calls 6

setMethod · 0.95
updateMetricsMethod · 0.95
BytesToStringFunction · 0.92
ErrPeerAlreadyExistsFunction · 0.85
EqualMethod · 0.80

Tested by 14

waitForTxInclusionFunction · 0.36
FuzzKeyDecodeEncodeFunction · 0.36
TestGetRandomFunction · 0.36
TestGetAllFunction · 0.36
TestAddRemoveHasFunction · 0.36
TestAddFailedDialAttemptFunction · 0.36
TestPeerSetAddGetDelFunction · 0.36
TestUpdateMustConnectsFunction · 0.36
TestEncryptedConnFunction · 0.36