2016-06-01 02:31:50 +08:00
|
|
|
package peerstore
|
2015-10-01 06:42:55 +08:00
|
|
|
|
|
|
|
import (
|
2016-08-20 05:16:56 +08:00
|
|
|
"fmt"
|
2015-10-01 06:42:55 +08:00
|
|
|
"sync"
|
|
|
|
|
2018-09-02 19:10:55 +08:00
|
|
|
"github.com/libp2p/go-libp2p-peer"
|
2015-10-01 06:42:55 +08:00
|
|
|
)
|
|
|
|
|
2018-08-29 22:12:41 +08:00
|
|
|
var _ Peerstore = (*peerstore)(nil)
|
2015-10-01 06:42:55 +08:00
|
|
|
|
|
|
|
type peerstore struct {
|
2018-08-29 22:12:41 +08:00
|
|
|
Metrics
|
2015-10-01 06:42:55 +08:00
|
|
|
|
2018-08-29 22:12:41 +08:00
|
|
|
KeyBook
|
|
|
|
AddrBook
|
|
|
|
PeerMetadata
|
2016-08-20 05:51:59 +08:00
|
|
|
|
|
|
|
// lock for protocol information, separate from datastore lock
|
|
|
|
protolock sync.Mutex
|
2015-10-01 06:42:55 +08:00
|
|
|
}
|
|
|
|
|
2018-08-29 22:12:41 +08:00
|
|
|
// NewPeerstore creates a threadsafe collection of peers.
|
|
|
|
func NewPeerstoreWith(kb KeyBook, ab AddrBook, md PeerMetadata) Peerstore {
|
|
|
|
return &peerstore{
|
|
|
|
KeyBook: kb,
|
|
|
|
PeerMetadata: md,
|
2018-08-31 01:05:22 +08:00
|
|
|
AddrBook: ab,
|
|
|
|
Metrics: NewMetrics(),
|
2015-11-19 03:47:51 +08:00
|
|
|
}
|
2015-10-01 06:42:55 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 02:31:50 +08:00
|
|
|
func (ps *peerstore) Peers() []peer.ID {
|
|
|
|
set := map[peer.ID]struct{}{}
|
2018-09-02 19:18:00 +08:00
|
|
|
for _, p := range ps.PeersWithKeys() {
|
2015-10-01 06:42:55 +08:00
|
|
|
set[p] = struct{}{}
|
|
|
|
}
|
2018-09-02 19:18:00 +08:00
|
|
|
for _, p := range ps.PeersWithAddrs() {
|
2015-10-01 06:42:55 +08:00
|
|
|
set[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2016-06-01 02:31:50 +08:00
|
|
|
pps := make([]peer.ID, 0, len(set))
|
2015-10-01 06:42:55 +08:00
|
|
|
for p := range set {
|
|
|
|
pps = append(pps, p)
|
|
|
|
}
|
|
|
|
return pps
|
|
|
|
}
|
|
|
|
|
2016-06-01 02:31:50 +08:00
|
|
|
func (ps *peerstore) PeerInfo(p peer.ID) PeerInfo {
|
2015-10-01 06:42:55 +08:00
|
|
|
return PeerInfo{
|
|
|
|
ID: p,
|
2018-06-14 07:27:14 +08:00
|
|
|
Addrs: ps.AddrBook.Addrs(p),
|
2015-10-01 06:42:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 07:49:21 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-20 05:16:56 +08:00
|
|
|
func (ps *peerstore) AddProtocols(p peer.ID, protos ...string) error {
|
2016-08-20 05:51:59 +08:00
|
|
|
ps.protolock.Lock()
|
|
|
|
defer ps.protolock.Unlock()
|
2016-08-20 05:16:56 +08:00
|
|
|
protomap, err := ps.getProtocolMap(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proto := range protos {
|
|
|
|
protomap[proto] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps.Put(p, "protocols", protomap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *peerstore) getProtocolMap(p peer.ID) (map[string]struct{}, error) {
|
|
|
|
iprotomap, err := ps.Get(p, "protocols")
|
|
|
|
switch err {
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
case ErrNotFound:
|
|
|
|
return make(map[string]struct{}), nil
|
|
|
|
case nil:
|
|
|
|
cast, ok := iprotomap.(map[string]struct{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("stored protocol set was not a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cast, nil
|
|
|
|
}
|
2016-06-11 07:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *peerstore) GetProtocols(p peer.ID) ([]string, error) {
|
2016-08-20 05:51:59 +08:00
|
|
|
ps.protolock.Lock()
|
|
|
|
defer ps.protolock.Unlock()
|
2016-08-20 05:16:56 +08:00
|
|
|
pmap, err := ps.getProtocolMap(p)
|
2016-06-11 07:13:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-20 05:16:56 +08:00
|
|
|
var out []string
|
|
|
|
for k, _ := range pmap {
|
|
|
|
out = append(out, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *peerstore) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) {
|
2016-08-20 05:51:59 +08:00
|
|
|
ps.protolock.Lock()
|
|
|
|
defer ps.protolock.Unlock()
|
2016-08-20 05:16:56 +08:00
|
|
|
pmap, err := ps.getProtocolMap(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
for _, proto := range protos {
|
|
|
|
if _, ok := pmap[proto]; ok {
|
|
|
|
out = append(out, proto)
|
|
|
|
}
|
2016-06-11 07:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-06-01 02:31:50 +08:00
|
|
|
func PeerInfos(ps Peerstore, peers []peer.ID) []PeerInfo {
|
2015-10-01 06:42:55 +08:00
|
|
|
pi := make([]PeerInfo, len(peers))
|
|
|
|
for i, p := range peers {
|
|
|
|
pi[i] = ps.PeerInfo(p)
|
|
|
|
}
|
|
|
|
return pi
|
|
|
|
}
|
|
|
|
|
2016-06-01 02:31:50 +08:00
|
|
|
func PeerInfoIDs(pis []PeerInfo) []peer.ID {
|
|
|
|
ps := make([]peer.ID, len(pis))
|
2015-10-01 06:42:55 +08:00
|
|
|
for i, pi := range pis {
|
|
|
|
ps[i] = pi.ID
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|