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"
|
2019-02-19 19:45:32 +08:00
|
|
|
"io"
|
2015-10-01 06:42:55 +08:00
|
|
|
|
2019-05-31 21:51:16 +08:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pstore "github.com/libp2p/go-libp2p-core/peerstore"
|
2015-10-01 06:42:55 +08:00
|
|
|
)
|
|
|
|
|
2019-05-31 21:51:16 +08:00
|
|
|
var _ pstore.Peerstore = (*peerstore)(nil)
|
2015-10-01 06:42:55 +08:00
|
|
|
|
|
|
|
type peerstore struct {
|
2019-05-31 21:51:16 +08:00
|
|
|
pstore.Metrics
|
2015-10-01 06:42:55 +08:00
|
|
|
|
2019-05-31 21:51:16 +08:00
|
|
|
pstore.KeyBook
|
|
|
|
pstore.AddrBook
|
|
|
|
pstore.ProtoBook
|
|
|
|
pstore.PeerMetadata
|
2015-10-01 06:42:55 +08:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:14:40 +08:00
|
|
|
// NewPeerstore creates a data structure that stores peer data, backed by the
|
|
|
|
// supplied implementations of KeyBook, AddrBook and PeerMetadata.
|
2020-03-06 20:07:36 +08:00
|
|
|
// Deprecated: use pstoreds.NewPeerstore or peerstoremem.NewPeerstore instead.
|
2019-05-31 21:51:16 +08:00
|
|
|
func NewPeerstore(kb pstore.KeyBook, ab pstore.AddrBook, pb pstore.ProtoBook, md pstore.PeerMetadata) pstore.Peerstore {
|
2018-08-29 22:12:41 +08:00
|
|
|
return &peerstore{
|
2019-05-09 17:14:03 +08:00
|
|
|
KeyBook: kb,
|
|
|
|
AddrBook: ab,
|
2019-05-17 17:33:33 +08:00
|
|
|
ProtoBook: pb,
|
2019-05-09 17:14:03 +08:00
|
|
|
PeerMetadata: md,
|
|
|
|
Metrics: NewMetrics(),
|
2015-11-19 03:47:51 +08:00
|
|
|
}
|
2015-10-01 06:42:55 +08:00
|
|
|
}
|
|
|
|
|
2019-02-19 19:45:32 +08:00
|
|
|
func (ps *peerstore) Close() (err error) {
|
2019-02-22 22:39:51 +08:00
|
|
|
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-02-19 19:45:32 +08:00
|
|
|
}
|
2019-02-22 22:39:51 +08:00
|
|
|
|
|
|
|
weakClose("keybook", ps.KeyBook)
|
|
|
|
weakClose("addressbook", ps.AddrBook)
|
2019-05-17 17:33:33 +08:00
|
|
|
weakClose("protobook", ps.ProtoBook)
|
2019-02-22 22:39:51 +08:00
|
|
|
weakClose("peermetadata", ps.PeerMetadata)
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("failed while closing peerstore; err(s): %q", errs)
|
2019-02-19 19:45:32 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-31 19:59:46 +08:00
|
|
|
func (ps *peerstore) Peers() peer.IDSlice {
|
2016-06-01 02:31:50 +08:00
|
|
|
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{}{}
|
|
|
|
}
|
|
|
|
|
2018-08-31 19:59:46 +08:00
|
|
|
pps := make(peer.IDSlice, 0, len(set))
|
2015-10-01 06:42:55 +08:00
|
|
|
for p := range set {
|
|
|
|
pps = append(pps, p)
|
|
|
|
}
|
|
|
|
return pps
|
|
|
|
}
|
|
|
|
|
2019-05-31 21:51:16 +08:00
|
|
|
func (ps *peerstore) PeerInfo(p peer.ID) peer.AddrInfo {
|
|
|
|
return peer.AddrInfo{
|
2015-10-01 06:42:55 +08:00
|
|
|
ID: p,
|
2018-06-14 07:27:14 +08:00
|
|
|
Addrs: ps.AddrBook.Addrs(p),
|
2015-10-01 06:42:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 21:51:16 +08:00
|
|
|
func PeerInfos(ps pstore.Peerstore, peers peer.IDSlice) []peer.AddrInfo {
|
|
|
|
pi := make([]peer.AddrInfo, len(peers))
|
2015-10-01 06:42:55 +08:00
|
|
|
for i, p := range peers {
|
|
|
|
pi[i] = ps.PeerInfo(p)
|
|
|
|
}
|
|
|
|
return pi
|
|
|
|
}
|
|
|
|
|
2019-05-31 21:51:16 +08:00
|
|
|
func PeerInfoIDs(pis []peer.AddrInfo) peer.IDSlice {
|
2018-08-31 19:59:46 +08:00
|
|
|
ps := make(peer.IDSlice, len(pis))
|
2015-10-01 06:42:55 +08:00
|
|
|
for i, pi := range pis {
|
|
|
|
ps[i] = pi.ID
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|