Skip to content

Commit

Permalink
feat: add peer connections subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jul 13, 2023
1 parent 430db08 commit 12a5956
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions app/client/cli/peer/connections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package peer

import (
"fmt"
"log"
"strconv"

libp2pNetwork "github.com/libp2p/go-libp2p/core/network"
"github.com/spf13/cobra"

"github.com/pokt-network/pocket/app/client/cli/helpers"
"github.com/pokt-network/pocket/p2p/utils"
"github.com/pokt-network/pocket/shared/modules"
)

var (
printConnectionsHeader = []string{"Peer ID", "Multiaddr", "Opened", "Direction", "NumStreams"}

connectionsCmd = &cobra.Command{
Use: "connections",
Short: "Print open peer connections",
RunE: connectionsRunE,
}
)

func init() {
PeerCmd.AddCommand(connectionsCmd)
}

func connectionsRunE(cmd *cobra.Command, _ []string) error {
bus, ok := helpers.GetValueFromCLIContext[modules.Bus](cmd, helpers.BusCLICtxKey)
if !ok {
log.Fatal("unable to get bus from context")
}

p2pModule := bus.GetP2PModule()
if p2pModule == nil {
return fmt.Errorf("no p2p module found on the bus")
}

conns := p2pModule.GetConnections()
if err := printConnections(conns); err != nil {
return fmt.Errorf("printing connections: %w", err)
}

return nil
}

func peerConnsRowConsumerFactory(conns []libp2pNetwork.Conn) utils.RowConsumer {
return func(provideRow utils.RowProvider) error {
for _, conn := range conns {
err := provideRow(
conn.RemotePeer().String(),
conn.RemoteMultiaddr().String(),
conn.Stat().Opened.String(),
conn.Stat().Direction.String(),
strconv.Itoa(conn.Stat().NumStreams),
)
if err != nil {
return err
}
}
return nil
}
}
func printConnections(conns []libp2pNetwork.Conn) error {
return utils.PrintTable(printConnectionsHeader, peerConnsRowConsumerFactory(conns))
}

0 comments on commit 12a5956

Please sign in to comment.