-
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.
- Loading branch information
1 parent
ff67b88
commit d1e811e
Showing
6 changed files
with
333 additions
and
6 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,95 @@ | ||
package peer | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
|
||
"github.com/pokt-network/pocket/app/client/cli/helpers" | ||
"github.com/pokt-network/pocket/logger" | ||
"github.com/pokt-network/pocket/p2p/debug" | ||
"github.com/pokt-network/pocket/shared/messaging" | ||
"github.com/pokt-network/pocket/shared/modules" | ||
) | ||
|
||
var ( | ||
listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Print addresses and service URLs of known peers", | ||
RunE: listRunE, | ||
} | ||
) | ||
|
||
func init() { | ||
PeerCmd.AddCommand(listCmd) | ||
} | ||
|
||
func listRunE(cmd *cobra.Command, _ []string) error { | ||
// TECHDEBT: factor out to a helper function `GetBusFromCmd()`. | ||
bus, ok := helpers.GetValueFromCLIContext[modules.Bus](cmd, helpers.BusCLICtxKey) | ||
if !ok { | ||
log.Fatal("unable to get bus from context") | ||
} | ||
|
||
var routerType debug.RouterType | ||
|
||
switch { | ||
case stakedFlag: | ||
routerType = debug.StakedRouterType | ||
case unstakedFlag: | ||
routerType = debug.UnstakedRouterType | ||
case allFlag: | ||
fallthrough | ||
default: | ||
routerType = debug.AllRouterTypes | ||
} | ||
|
||
debugMsg := &messaging.DebugMessage{ | ||
Action: messaging.DebugMessageAction_DEBUG_P2P_PEER_LIST, | ||
Type: messaging.DebugMessageRoutingType_DEBUG_MESSAGE_TYPE_BROADCAST, | ||
Message: &anypb.Any{ | ||
Value: []byte(routerType), | ||
}, | ||
} | ||
debugMsgAny, err := anypb.New(debugMsg) | ||
if err != nil { | ||
return fmt.Errorf("creating anypb from debug message: %w", err) | ||
} | ||
|
||
if localFlag { | ||
// call common behavior -- debug message handler | ||
panic("not implemented") | ||
} | ||
|
||
// // TECHDEBT: will need to wait for DHT bootstrapping to complete before | ||
// // this command can be used with unstaked actors. | ||
// | ||
// if err := bus.GetP2PModule().Broadcast(debugMsgAny); err != nil { | ||
// return fmt.Errorf("broadcasting debug message: %w", err) | ||
// } | ||
|
||
// TECHDEBT: use broadcast instead once the above TECHDEBT is resolved. | ||
pstore, err := helpers.FetchPeerstore(cmd) | ||
if err != nil { | ||
logger.Global.Fatal().Err(err).Msg("Unable to retrieve the pstore") | ||
} | ||
|
||
if pstore.Size() == 0 { | ||
logger.Global.Fatal().Msg("No validators found") | ||
} | ||
|
||
peers := pstore.GetPeerList() | ||
if err != nil { | ||
logger.Global.Fatal().Err(err).Msg("Failed to convert validator address into pocketCrypto.Address") | ||
} | ||
|
||
for _, peer := range peers { | ||
if err := bus.GetP2PModule().Send(peer.GetAddress(), debugMsgAny); err != nil { | ||
logger.Global.Error().Err(err).Msg("Failed to send debug message") | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,104 @@ | ||
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" | ||
) | ||
|
||
func (m *p2pModule) handleDebugMessage(msg *messaging.DebugMessage) error { | ||
switch msg.Action { | ||
case messaging.DebugMessageAction_DEBUG_P2P_PEER_LIST: | ||
if !m.cfg.EnablePeerDiscoveryDebugRpc { | ||
return typesP2P.ErrPeerDiscoveryDebugRPCDisabled | ||
} | ||
} | ||
|
||
// TODO_THIS_COMMIT: add & react to `--all`, `--staked`, `--unstaked` | ||
// persistent flags | ||
|
||
switch msg.Action { | ||
case messaging.DebugMessageAction_DEBUG_P2P_PEER_LIST: | ||
routerType := debug.RouterType(msg.Message.Value) | ||
return m.listPeers(routerType) | ||
default: | ||
return fmt.Errorf("unsupported P2P debug message action: %s", msg.Action) | ||
} | ||
} | ||
|
||
func (m *p2pModule) listPeers(routerType debug.RouterType) error { | ||
var ( | ||
peers typesP2P.PeerList | ||
pstorePlurality = "" | ||
) | ||
|
||
switch routerType { | ||
case debug.StakedRouterType: | ||
// TECHDEBT: add `PeerstoreProvider#GetStakedPeerstoreAtCurrentHeight()` | ||
// interface method. | ||
currentHeight := m.currentHeightProvider.CurrentHeight() | ||
pstore, err := m.pstoreProvider.GetStakedPeerstoreAtHeight(currentHeight) | ||
if err != nil { | ||
return fmt.Errorf("getting unstaked peerstore: %v", err) | ||
} | ||
|
||
peers = pstore.GetPeerList() | ||
case debug.UnstakedRouterType: | ||
pstore, err := m.pstoreProvider.GetUnstakedPeerstore() | ||
if err != nil { | ||
return fmt.Errorf("getting unstaked peerstore: %v", err) | ||
} | ||
|
||
peers = pstore.GetPeerList() | ||
case debug.AllRouterTypes: | ||
pstorePlurality = "s" | ||
|
||
// TECHDEBT: add `PeerstoreProvider#GetStakedPeerstoreAtCurrentHeight()` | ||
currentHeight := m.currentHeightProvider.CurrentHeight() | ||
stakedPStore, err := m.pstoreProvider.GetStakedPeerstoreAtHeight(currentHeight) | ||
if err != nil { | ||
return fmt.Errorf("getting unstaked peerstore: %v", err) | ||
} | ||
unstakedPStore, err := m.pstoreProvider.GetUnstakedPeerstore() | ||
if err != nil { | ||
return fmt.Errorf("getting unstaked peerstore: %v", err) | ||
} | ||
|
||
unstakedPeers := unstakedPStore.GetPeerList() | ||
stakedPeers := stakedPStore.GetPeerList() | ||
additionalPeers, _ := unstakedPeers.Delta(stakedPeers) | ||
|
||
// NB: there should never be any "additional" peers. This would represent | ||
// a staked actor who is not participating in background gossip for some | ||
// reason. It's possible that a staked actor node which has restarted | ||
// recently and hasn't yet completed background router bootstrapping may | ||
// result in peers experiencing this state. | ||
if len(additionalPeers) == 0 { | ||
return debug.PrintPeerList(unstakedPeers) | ||
} | ||
|
||
var allPeers typesP2P.PeerList | ||
for _, peer := range additionalPeers { | ||
allPeers = append(unstakedPeers, peer) | ||
} | ||
peers = allPeers | ||
default: | ||
return fmt.Errorf("unsupported router type: %s", routerType) | ||
} | ||
|
||
if err := debug.LogSelfAddress(m.logger, m.GetBus()); err != nil { | ||
return fmt.Errorf("printing self address: %w", err) | ||
} | ||
|
||
// NB: Intentionally printing with `fmt` instead of the logger to match | ||
// `utils.PrintPeerList` which does not use the logger due to | ||
// incompatibilities with the tabwriter. | ||
fmt.Printf("%s router peerstore%s:\n", routerType, pstorePlurality) | ||
|
||
if err := debug.PrintPeerList(peers); err != nil { | ||
return fmt.Errorf("printing peer list: %w", err) | ||
} | ||
return nil | ||
} |
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,61 @@ | ||
package debug | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pokt-network/pocket/p2p/types" | ||
"github.com/pokt-network/pocket/p2p/utils" | ||
"github.com/pokt-network/pocket/shared/modules" | ||
) | ||
|
||
type RouterType string | ||
|
||
const ( | ||
StakedRouterType RouterType = "staked" | ||
UnstakedRouterType RouterType = "unstaked" | ||
AllRouterTypes RouterType = "all" | ||
) | ||
|
||
func LogSelfAddress(logger *modules.Logger, bus modules.Bus) error { | ||
p2pModule := bus.GetP2PModule() | ||
if p2pModule == nil { | ||
return fmt.Errorf("no p2p module found on the bus") | ||
} | ||
|
||
selfAddr, err := p2pModule.GetAddress() | ||
if err != nil { | ||
return fmt.Errorf("getting self address: %w", err) | ||
} | ||
|
||
logger.Debug().Str("self_address", selfAddr.String()).Msg("") | ||
return nil | ||
} | ||
|
||
// PrintPeerList prints a table of the passed peers to stdout. Header row is defined | ||
// by `peerListTableHeader`. Row printing behavior is defined by `peerListRowConsumerFactory`. | ||
func PrintPeerList(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 | ||
} | ||
} | ||
|
||
var peerListTableHeader = []string{"Peer ID", "Pokt Address", "ServiceURL"} |
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
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
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