2018-07-04 10:51:47 +00:00
|
|
|
package peerstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-09 07:24:20 +00:00
|
|
|
"io"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pstore "github.com/libp2p/go-libp2p-core/peerstore"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
var _ pstore.Peerstore = (*peerstore)(nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
type peerstore struct {
|
2019-10-04 15:21:24 +00:00
|
|
|
pstore.Metrics
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
pstore.KeyBook
|
|
|
|
pstore.AddrBook
|
|
|
|
pstore.ProtoBook
|
|
|
|
pstore.PeerMetadata
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2019-10-04 15:21:24 +00:00
|
|
|
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(),
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
weakClose("keybook", ps.KeyBook)
|
|
|
|
weakClose("addressbook", ps.AddrBook)
|
|
|
|
weakClose("protobook", ps.ProtoBook)
|
|
|
|
weakClose("peermetadata", ps.PeerMetadata)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("failed while closing peerstore; err(s): %q", errs)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func (ps *peerstore) Peers() peer.IDSlice {
|
2018-07-04 10:51:47 +00:00
|
|
|
set := map[peer.ID]struct{}{}
|
2019-06-09 07:24:20 +00:00
|
|
|
for _, p := range ps.PeersWithKeys() {
|
2018-07-04 10:51:47 +00:00
|
|
|
set[p] = struct{}{}
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
for _, p := range ps.PeersWithAddrs() {
|
2018-07-04 10:51:47 +00:00
|
|
|
set[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
pps := make(peer.IDSlice, 0, len(set))
|
2018-07-04 10:51:47 +00:00
|
|
|
for p := range set {
|
|
|
|
pps = append(pps, p)
|
|
|
|
}
|
|
|
|
return pps
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
func (ps *peerstore) PeerInfo(p peer.ID) peer.AddrInfo {
|
|
|
|
return peer.AddrInfo{
|
2018-07-04 10:51:47 +00:00
|
|
|
ID: p,
|
2019-06-09 07:24:20 +00:00
|
|
|
Addrs: ps.AddrBook.Addrs(p),
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
func PeerInfos(ps pstore.Peerstore, peers peer.IDSlice) []peer.AddrInfo {
|
|
|
|
pi := make([]peer.AddrInfo, len(peers))
|
2018-07-04 10:51:47 +00:00
|
|
|
for i, p := range peers {
|
|
|
|
pi[i] = ps.PeerInfo(p)
|
|
|
|
}
|
|
|
|
return pi
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
func PeerInfoIDs(pis []peer.AddrInfo) peer.IDSlice {
|
2019-06-09 07:24:20 +00:00
|
|
|
ps := make(peer.IDSlice, len(pis))
|
2018-07-04 10:51:47 +00:00
|
|
|
for i, pi := range pis {
|
|
|
|
ps[i] = pi.ID
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|