Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
feat: add helper functions for working with addr infos (#202)
Browse files Browse the repository at this point in the history
Specifically, move them _here_ from the peerstore. That way
packages (like the DHT) that currently directly rely on the peerstore,
can just use go-libp2p-core.

Moved from https://github.com/libp2p/go-libp2p-peerstore/blob/f7f22569f7d49635953638ffb11915dd3d054cf5/peerstore.go#L79-L93

With some small modifications.
  • Loading branch information
Stebalien committed Jul 21, 2021
1 parent ef6e277 commit 8c04ed1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions peer/addrinfo.go
Expand Up @@ -106,3 +106,12 @@ func (pi *AddrInfo) Loggable() map[string]interface{} {
"addrs": pi.Addrs,
}
}

// AddrInfosToIDs extracts the peer IDs from the passed AddrInfos and returns them in-order.
func AddrInfosToIDs(pis []AddrInfo) []ID {
ps := make([]ID, len(pis))
for i, pi := range pis {
ps[i] = pi.ID
}
return ps
}
14 changes: 14 additions & 0 deletions peerstore/helpers.go
@@ -0,0 +1,14 @@
package peerstore

import (
"github.com/libp2p/go-libp2p-core/peer"
)

// AddrInfos returns an AddrInfo for each specified peer ID, in-order.
func AddrInfos(ps Peerstore, peers []peer.ID) []peer.AddrInfo {
pi := make([]peer.AddrInfo, len(peers))
for i, p := range peers {
pi[i] = ps.PeerInfo(p)
}
return pi
}

0 comments on commit 8c04ed1

Please sign in to comment.