mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-13 11:30:09 +08:00
Move Peers from Peerstore to AddrBook
This commit is contained in:
parent
e8fbef6e78
commit
00793e6244
@ -39,6 +39,11 @@ func peerAddressKey(p *peer.ID, addr *ma.Multiaddr) (ds.Key, error) {
|
|||||||
return ds.NewKey(p.Pretty()).ChildString(hash.B58String()), nil
|
return ds.NewKey(p.Pretty()).ChildString(hash.B58String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func peerIDFromKey(key ds.Key) (peer.ID, error) {
|
||||||
|
idstring := key.Parent().Type()
|
||||||
|
return peer.IDB58Decode(idstring)
|
||||||
|
}
|
||||||
|
|
||||||
func (mgr *DatastoreAddrManager) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
|
func (mgr *DatastoreAddrManager) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
|
||||||
mgr.AddAddrs(p, []ma.Multiaddr{addr}, ttl)
|
mgr.AddAddrs(p, []ma.Multiaddr{addr}, ttl)
|
||||||
}
|
}
|
||||||
@ -110,6 +115,31 @@ func (mgr *DatastoreAddrManager) Addrs(p peer.ID) []ma.Multiaddr {
|
|||||||
return addrs
|
return addrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mgr *DatastoreAddrManager) Peers() []peer.ID {
|
||||||
|
q := query.Query{}
|
||||||
|
results, err := mgr.ds.Query(q)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return []peer.ID{}
|
||||||
|
}
|
||||||
|
|
||||||
|
idset := make(map[peer.ID]struct{})
|
||||||
|
for result := range results.Next() {
|
||||||
|
key := ds.RawKey(result.Key)
|
||||||
|
id, err := peerIDFromKey(key)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
idset[id] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
ids := make([]peer.ID, 0, len(idset))
|
||||||
|
for id := range idset {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
func (mgr *DatastoreAddrManager) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
|
func (mgr *DatastoreAddrManager) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
|
||||||
initial := mgr.Addrs(p)
|
initial := mgr.Addrs(p)
|
||||||
return mgr.subsManager.AddrStream(ctx, p, initial)
|
return mgr.subsManager.AddrStream(ctx, p, initial)
|
||||||
|
12
peerstore.go
12
peerstore.go
@ -30,9 +30,6 @@ type Peerstore interface {
|
|||||||
KeyBook
|
KeyBook
|
||||||
Metrics
|
Metrics
|
||||||
|
|
||||||
// Peers returns a list of all peer.IDs in this Peerstore
|
|
||||||
Peers() []peer.ID
|
|
||||||
|
|
||||||
// PeerInfo returns a peer.PeerInfo struct for given peer.ID.
|
// PeerInfo returns a peer.PeerInfo struct for given peer.ID.
|
||||||
// This is a small slice of the information Peerstore has on
|
// This is a small slice of the information Peerstore has on
|
||||||
// that peer, useful to other services.
|
// that peer, useful to other services.
|
||||||
@ -83,6 +80,9 @@ type AddrBook interface {
|
|||||||
|
|
||||||
// ClearAddresses removes all previously stored addresses
|
// ClearAddresses removes all previously stored addresses
|
||||||
ClearAddrs(p peer.ID)
|
ClearAddrs(p peer.ID)
|
||||||
|
|
||||||
|
// Peers returns all of the peer IDs stored in the AddrBook
|
||||||
|
Peers() []peer.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyBook tracks the Public keys of Peers.
|
// KeyBook tracks the Public keys of Peers.
|
||||||
@ -179,7 +179,7 @@ func (kb *keybook) AddPrivKey(p peer.ID, sk ic.PrivKey) error {
|
|||||||
type peerstore struct {
|
type peerstore struct {
|
||||||
*keybook
|
*keybook
|
||||||
*metrics
|
*metrics
|
||||||
AddrManager
|
AddrBook
|
||||||
|
|
||||||
// store other data, like versions
|
// store other data, like versions
|
||||||
//ds ds.ThreadSafeDatastore
|
//ds ds.ThreadSafeDatastore
|
||||||
@ -231,7 +231,7 @@ func (ps *peerstore) Peers() []peer.ID {
|
|||||||
for _, p := range ps.keybook.Peers() {
|
for _, p := range ps.keybook.Peers() {
|
||||||
set[p] = struct{}{}
|
set[p] = struct{}{}
|
||||||
}
|
}
|
||||||
for _, p := range ps.AddrManager.Peers() {
|
for _, p := range ps.AddrBook.Peers() {
|
||||||
set[p] = struct{}{}
|
set[p] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ func (ps *peerstore) Peers() []peer.ID {
|
|||||||
func (ps *peerstore) PeerInfo(p peer.ID) PeerInfo {
|
func (ps *peerstore) PeerInfo(p peer.ID) PeerInfo {
|
||||||
return PeerInfo{
|
return PeerInfo{
|
||||||
ID: p,
|
ID: p,
|
||||||
Addrs: ps.AddrManager.Addrs(p),
|
Addrs: ps.AddrBook.Addrs(p),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user