p2p/protocols, swarm/network/stream: add protocols.Peer.HasCap

This commit is contained in:
Janos Guljas 2019-01-22 12:05:31 +01:00
parent 6dd6fa17d8
commit 425acb3016
3 changed files with 13 additions and 12 deletions

View file

@ -423,3 +423,14 @@ func (p *Peer) Handshake(ctx context.Context, hs interface{}, verify func(interf
}
return rhs, nil
}
// HasCap returns true fi Peer has a capability
// with provided name.
func (p *Peer) HasCap(capName string) (yes bool) {
for _, c := range p.Caps() {
if c.Name == capName {
return true
}
}
return false
}

View file

@ -261,7 +261,7 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
}
// nodes that do not provide stream protocol
// should not be requested, e.g. bootnodes
if !hasStreamCap(sp.Peer.Peer) {
if !p.HasCap("stream") {
return true
}
spID = &id

View file

@ -518,7 +518,7 @@ func (r *Registry) requestPeerSubscriptions(kad *network.Kademlia, subs map[enod
kad.EachConn(nil, 255, func(p *network.Peer, po int) bool {
// nodes that do not provide stream protocol
// should not be subscribed, e.g. bootnodes
if !hasStreamCap(p.Peer.Peer) {
if !p.HasCap("stream") {
return true
}
//if the peer's bin is shallower than the kademlia depth,
@ -929,13 +929,3 @@ func (api *API) SubscribeStream(peerId enode.ID, s Stream, history *Range, prior
func (api *API) UnsubscribeStream(peerId enode.ID, s Stream) error {
return api.streamer.Unsubscribe(peerId, s)
}
// hasStreamCap check if p2p.Peer the "stream" capability.
func hasStreamCap(p *p2p.Peer) (yes bool) {
for _, c := range p.Caps() {
if c.Name == "stream" {
return true
}
}
return false
}