mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-25 13:20:07 +08:00
Add SetProtocols method to peerstore
This allows us to overwrite known protocols and remove potentially unwanted ones during a full knowledge update. The current AddProtocol just appends to the existing set.
This commit is contained in:
parent
639e4e1e92
commit
f759b0e611
13
peerstore.go
13
peerstore.go
@ -46,6 +46,7 @@ type Peerstore interface {
|
||||
|
||||
GetProtocols(peer.ID) ([]string, error)
|
||||
AddProtocols(peer.ID, ...string) error
|
||||
SetProtocols(peer.ID, ...string) error
|
||||
SupportsProtocols(peer.ID, ...string) ([]string, error)
|
||||
}
|
||||
|
||||
@ -235,6 +236,18 @@ func (ps *peerstore) PeerInfo(p peer.ID) PeerInfo {
|
||||
}
|
||||
}
|
||||
|
||||
func (ps *peerstore) SetProtocols(p peer.ID, protos ...string) error {
|
||||
ps.protolock.Lock()
|
||||
defer ps.protolock.Unlock()
|
||||
|
||||
protomap := make(map[string]struct{})
|
||||
for _, proto := range protos {
|
||||
protomap[proto] = struct{}{}
|
||||
}
|
||||
|
||||
return ps.Put(p, "protocols", protomap)
|
||||
}
|
||||
|
||||
func (ps *peerstore) AddProtocols(p peer.ID, protos ...string) error {
|
||||
ps.protolock.Lock()
|
||||
defer ps.protolock.Unlock()
|
||||
|
@ -220,6 +220,20 @@ func TestPeerstoreProtoStore(t *testing.T) {
|
||||
if supported[0] != "a" || supported[1] != "b" {
|
||||
t.Fatal("got wrong supported array: ", supported)
|
||||
}
|
||||
|
||||
err = ps.SetProtocols(p1, "other")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
supported, err = ps.SupportsProtocols(p1, "q", "w", "a", "y", "b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(supported) != 0 {
|
||||
t.Fatal("none of those protocols should have been supported")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasicPeerstore(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user