-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
peer connections
subcommand
- Loading branch information
1 parent
430db08
commit 12a5956
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |