Skip to content

Commit

Permalink
Add generators of protected, unprotected connected peers
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Nov 27, 2023
1 parent 3b43c67 commit a0d0ed7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
62 changes: 52 additions & 10 deletions p2p/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,68 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
)

// Get list of connected peers for the given host
func (p *P2P) GetConnectedPeers() map[string]*peer.AddrInfo {
// GetAllConnectedPeers returns a slice of peer.ID for all connected peers of the given host.
func (p *P2P) GetAllConnectedPeers() peer.IDSlice {
h := p.Node
var connectedPeers peer.IDSlice

for _, p := range h.Network().Peers() {
if h.Network().Connectedness(p) == network.Connected {
connectedPeers = append(connectedPeers, p)
}
}

return connectedPeers
}

// GetConnectedProtectedPeers returns a slice of peer.ID for all protected connected peers.
func (p *P2P) GetConnectedProtectedPeers() peer.IDSlice {
h := p.Node
var connectedProtectedPeers peer.IDSlice

connectedPeers := make(map[string]*peer.AddrInfo)
for _, connectedPeer := range p.GetAllConnectedPeers() {
if h.ConnManager().IsProtected(connectedPeer, ma.RENDEZVOUS) {
connectedProtectedPeers = append(connectedProtectedPeers, connectedPeer)
}
}

for _, p := range h.Network().Peers() {
return connectedProtectedPeers
}

if h.ConnManager().IsProtected(p, ma.RENDEZVOUS) {
// GetConnectedUnprotectedPeers returns a slice of peer.ID for all unprotected connected peers.
func (p *P2P) GetConnectedUnprotectedPeers() peer.IDSlice {
connectedPeers := p.GetAllConnectedPeers()
connectedProtectedPeers := p.GetConnectedProtectedPeers()

if h.Network().Connectedness(p) == network.Connected {
var connectedUnprotectedPeers peer.IDSlice
for _, connectedPeer := range connectedPeers {
if !containsPeer(connectedProtectedPeers, connectedPeer) {
connectedUnprotectedPeers = append(connectedUnprotectedPeers, connectedPeer)
}
}

connectedPeer := h.Peerstore().PeerInfo(p)
return connectedUnprotectedPeers
}

connectedPeers[p.String()] = &connectedPeer
}
// containsPeer checks if a peer.ID is present in a slice of peer.ID.
func containsPeer(slice peer.IDSlice, peerID peer.ID) bool {
for _, p := range slice {
if p == peerID {
return true
}
}
return false
}

// GetConnectedProtectedPeersAddrInfo returns a map of peer.ID to AddrInfo for all protected connected peers.
func (p *P2P) GetConnectedProtectedPeersAddrInfo() map[string]*peer.AddrInfo {
h := p.Node
connectedPeersAddrInfo := make(map[string]*peer.AddrInfo)

for _, connectedPeer := range p.GetConnectedProtectedPeers() {
peerAddrInfo := h.Peerstore().PeerInfo(connectedPeer)
connectedPeersAddrInfo[connectedPeer.String()] = &peerAddrInfo
}

return connectedPeers
return connectedPeersAddrInfo
}
2 changes: 1 addition & 1 deletion ui/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (ui *ChatUI) handleAliasCommand(args []string) {
func (ui *ChatUI) refreshPeers() {

// Tweak this to change the timeout for peer discovery
peers := ui.p.GetConnectedPeers()
peers := ui.p.GetConnectedProtectedPeersAddrInfo()

// clear is thread-safe
ui.peersList.Clear()
Expand Down

0 comments on commit a0d0ed7

Please sign in to comment.