status-go/vendor/github.com/libp2p/go-libp2p-peerstore/peerstore.go

93 lines
1.9 KiB
Go
Raw Normal View History

package peerstore
import (
"fmt"
2019-06-09 07:24:20 +00:00
"io"
"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
)
var _ pstore.Peerstore = (*peerstore)(nil)
2019-06-09 07:24:20 +00:00
type peerstore struct {
pstore.Metrics
pstore.KeyBook
pstore.AddrBook
pstore.ProtoBook
pstore.PeerMetadata
}
2019-06-09 07:24:20 +00:00
// NewPeerstore creates a data structure that stores peer data, backed by the
// supplied implementations of KeyBook, AddrBook and PeerMetadata.
func NewPeerstore(kb pstore.KeyBook, ab pstore.AddrBook, pb pstore.ProtoBook, md pstore.PeerMetadata) pstore.Peerstore {
2019-06-09 07:24:20 +00:00
return &peerstore{
KeyBook: kb,
AddrBook: ab,
ProtoBook: pb,
PeerMetadata: md,
Metrics: NewMetrics(),
}
}
2019-06-09 07:24:20 +00:00
func (ps *peerstore) Close() (err error) {
var errs []error
weakClose := func(name string, c interface{}) {
if cl, ok := c.(io.Closer); ok {
if err = cl.Close(); err != nil {
errs = append(errs, fmt.Errorf("%s error: %s", name, err))
}
}
}
2019-06-09 07:24:20 +00:00
weakClose("keybook", ps.KeyBook)
weakClose("addressbook", ps.AddrBook)
weakClose("protobook", ps.ProtoBook)
weakClose("peermetadata", ps.PeerMetadata)
2019-06-09 07:24:20 +00:00
if len(errs) > 0 {
return fmt.Errorf("failed while closing peerstore; err(s): %q", errs)
}
return nil
}
2019-06-09 07:24:20 +00:00
func (ps *peerstore) Peers() peer.IDSlice {
set := map[peer.ID]struct{}{}
2019-06-09 07:24:20 +00:00
for _, p := range ps.PeersWithKeys() {
set[p] = struct{}{}
}
2019-06-09 07:24:20 +00:00
for _, p := range ps.PeersWithAddrs() {
set[p] = struct{}{}
}
2019-06-09 07:24:20 +00:00
pps := make(peer.IDSlice, 0, len(set))
for p := range set {
pps = append(pps, p)
}
return pps
}
func (ps *peerstore) PeerInfo(p peer.ID) peer.AddrInfo {
return peer.AddrInfo{
ID: p,
2019-06-09 07:24:20 +00:00
Addrs: ps.AddrBook.Addrs(p),
}
}
func PeerInfos(ps pstore.Peerstore, peers peer.IDSlice) []peer.AddrInfo {
pi := make([]peer.AddrInfo, len(peers))
for i, p := range peers {
pi[i] = ps.PeerInfo(p)
}
return pi
}
func PeerInfoIDs(pis []peer.AddrInfo) peer.IDSlice {
2019-06-09 07:24:20 +00:00
ps := make(peer.IDSlice, len(pis))
for i, pi := range pis {
ps[i] = pi.ID
}
return ps
}