Commit 7a58f873 authored by Aarsh Shah's avatar Aarsh Shah

peer supports any protocol

parent b1f58c03
......@@ -122,6 +122,28 @@ func (pb *dsProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string,
return res, nil
}
func (pb *dsProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, error) {
if err := p.Validate(); err != nil {
return false, err
}
s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()
pmap, err := pb.getProtocolMap(p)
if err != nil {
return false, err
}
for _, proto := range protos {
if _, ok := pmap[proto]; ok {
return true, nil
}
}
return false, nil
}
func (pb *dsProtoBook) RemoveProtocols(p peer.ID, protos ...string) error {
if err := p.Validate(); err != nil {
return err
......
......@@ -163,3 +163,20 @@ func (pb *memoryProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]str
return out, nil
}
func (pb *memoryProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, error) {
if err := p.Validate(); err != nil {
return false, err
}
s := pb.segments.get(p)
s.RLock()
defer s.RUnlock()
for _, proto := range protos {
if _, ok := s.protocols[p][proto]; ok {
return true, nil
}
}
return false, nil
}
......@@ -14,6 +14,7 @@ import (
ma "github.com/multiformats/go-multiaddr"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
"github.com/stretchr/testify/require"
)
var peerstoreSuite = map[string]func(pstore.Peerstore) func(*testing.T){
......@@ -242,6 +243,18 @@ func testPeerstoreProtoStore(ps pstore.Peerstore) func(t *testing.T) {
t.Fatal("got wrong supported array: ", supported)
}
b, err := ps.SupportsAnyProtocol(p1, "q", "w", "a", "y", "b")
require.NoError(t, err)
require.True(t, b)
b, err = ps.SupportsAnyProtocol(p1, "a")
require.NoError(t, err)
require.True(t, b)
b, err = ps.SupportsAnyProtocol(p1, "q")
require.NoError(t, err)
require.False(t, b)
protos = []string{"other", "yet another", "one more"}
err = ps.SetProtocols(p1, protos...)
if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment