Skip to content

Commit

Permalink
fixup: add peer list subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jul 13, 2023
1 parent 3925c71 commit 1bbad38
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
5 changes: 2 additions & 3 deletions app/client/cli/peer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ func listRunE(cmd *cobra.Command, _ []string) error {
return ErrRouterType
}
routerType = debug.UnstakedRouterType
case allFlag:
// even if `allFlag` is false, we still want to print all peers
default:
if stakedFlag || unstakedFlag {
return ErrRouterType
}
routerType = debug.AllRouterTypes
default:
return ErrRouterType
}

debugMsg := &messaging.DebugMessage{
Expand Down
2 changes: 1 addition & 1 deletion app/client/cli/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
)

func init() {
PeerCmd.PersistentFlags().BoolVarP(&allFlag, "all", "a", true, "operations apply to both staked & unstaked router peerstores")
PeerCmd.PersistentFlags().BoolVarP(&allFlag, "all", "a", false, "operations apply to both staked & unstaked router peerstores (default)")
PeerCmd.PersistentFlags().BoolVarP(&stakedFlag, "staked", "s", false, "operations only apply to staked router peerstore (i.e. raintree)")
PeerCmd.PersistentFlags().BoolVarP(&unstakedFlag, "unstaked", "u", false, "operations only apply to unstaked router peerstore (i.e. gossipsub)")
PeerCmd.PersistentFlags().BoolVarP(&localFlag, "local", "l", false, "operations apply to the local (CLI binary's) P2P module instead of being broadcast")
Expand Down
1 change: 1 addition & 0 deletions p2p/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package p2p

import (
"fmt"

"github.com/pokt-network/pocket/p2p/debug"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/shared/messaging"
Expand Down
39 changes: 33 additions & 6 deletions p2p/debug/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package debug

import (
"fmt"
"os"

"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
"github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/p2p/utils"
"github.com/pokt-network/pocket/shared/modules"
"os"
)

var peerListTableHeader = []string{"Peer ID", "Pokt Address", "ServiceURL"}

func PrintPeerList(bus modules.Bus, routerType RouterType) error {
var (
peers types.PeerList
pstorePlurality = ""
routerPlurality = ""
)

// TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider
Expand Down Expand Up @@ -45,7 +49,7 @@ func PrintPeerList(bus modules.Bus, routerType RouterType) error {

peers = pstore.GetPeerList()
case AllRouterTypes:
pstorePlurality = "s"
routerPlurality = "s"

// TODO_THIS_COMMIT: what about unstaked peers actors?
// if !isStaked ...
Expand Down Expand Up @@ -90,7 +94,7 @@ func PrintPeerList(bus modules.Bus, routerType RouterType) error {
os.Stdout,
"%s router peerstore%s:\n",
routerType,
pstorePlurality,
routerPlurality,
); err != nil {
return fmt.Errorf("printing to stdout: %w", err)
}
Expand All @@ -101,6 +105,29 @@ func PrintPeerList(bus modules.Bus, routerType RouterType) error {
return nil
}

func getPeerstoreProvider() (peerstore_provider.PeerstoreProvider, error) {
return nil, nil
// PrintPeerListTable prints a table of the passed peers to stdout. Header row is defined
// by `peerListTableHeader`. Row printing behavior is defined by `peerListRowConsumerFactory`.
func PrintPeerListTable(peers types.PeerList) error {
return utils.PrintTable(peerListTableHeader, peerListRowConsumerFactory(peers))
}

func peerListRowConsumerFactory(peers types.PeerList) utils.RowConsumer {
return func(provideRow utils.RowProvider) error {
for _, peer := range peers {
libp2pAddrInfo, err := utils.Libp2pAddrInfoFromPeer(peer)
if err != nil {
return fmt.Errorf("converting peer to libp2p addr info: %w", err)
}

err = provideRow(
libp2pAddrInfo.ID.String(),
peer.GetAddress().String(),
peer.GetServiceURL(),
)
if err != nil {
return err
}
}
return nil
}
}
31 changes: 0 additions & 31 deletions p2p/debug/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"os"

"github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/p2p/utils"
"github.com/pokt-network/pocket/shared/modules"
)

Expand All @@ -17,8 +15,6 @@ const (
AllRouterTypes RouterType = "all"
)

var peerListTableHeader = []string{"Peer ID", "Pokt Address", "ServiceURL"}

func LogSelfAddress(bus modules.Bus) error {
p2pModule := bus.GetP2PModule()
if p2pModule == nil {
Expand All @@ -33,30 +29,3 @@ func LogSelfAddress(bus modules.Bus) error {
_, err = fmt.Fprintf(os.Stdout, "self address: %s", selfAddr.String())
return err
}

// PrintPeerListTable prints a table of the passed peers to stdout. Header row is defined
// by `peerListTableHeader`. Row printing behavior is defined by `peerListRowConsumerFactory`.
func PrintPeerListTable(peers types.PeerList) error {
return utils.PrintTable(peerListTableHeader, peerListRowConsumerFactory(peers))
}

func peerListRowConsumerFactory(peers types.PeerList) utils.RowConsumer {
return func(provideRow utils.RowProvider) error {
for _, peer := range peers {
libp2pAddrInfo, err := utils.Libp2pAddrInfoFromPeer(peer)
if err != nil {
return fmt.Errorf("converting peer to libp2p addr info: %w", err)
}

err = provideRow(
libp2pAddrInfo.ID.String(),
peer.GetAddress().String(),
peer.GetServiceURL(),
)
if err != nil {
return err
}
}
return nil
}
}

0 comments on commit 1bbad38

Please sign in to comment.