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

Commit 8c04ed1

Browse files
authored
feat: add helper functions for working with addr infos (#202)
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.
1 parent ef6e277 commit 8c04ed1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

peer/addrinfo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,12 @@ func (pi *AddrInfo) Loggable() map[string]interface{} {
106106
"addrs": pi.Addrs,
107107
}
108108
}
109+
110+
// AddrInfosToIDs extracts the peer IDs from the passed AddrInfos and returns them in-order.
111+
func AddrInfosToIDs(pis []AddrInfo) []ID {
112+
ps := make([]ID, len(pis))
113+
for i, pi := range pis {
114+
ps[i] = pi.ID
115+
}
116+
return ps
117+
}

peerstore/helpers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package peerstore
2+
3+
import (
4+
"github.com/libp2p/go-libp2p-core/peer"
5+
)
6+
7+
// AddrInfos returns an AddrInfo for each specified peer ID, in-order.
8+
func AddrInfos(ps Peerstore, peers []peer.ID) []peer.AddrInfo {
9+
pi := make([]peer.AddrInfo, len(peers))
10+
for i, p := range peers {
11+
pi[i] = ps.PeerInfo(p)
12+
}
13+
return pi
14+
}

0 commit comments

Comments
 (0)