-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[P2P] [Tooling] Peer discovery peer list
subcommand
#892
base: main
Are you sure you want to change the base?
Changes from all commits
4badf3a
2b83d32
eac7695
bca000b
5e963be
a883f08
83c3604
6ecca53
d570b35
e952365
e80843c
8f90e22
6e691cd
430db08
440b59a
fcfa837
04dc0aa
3925c71
1bbad38
64abbc0
d8b6296
9ecc9e5
1cbc249
ffbc539
0cff1d2
39af37c
c22011c
1fc2bb4
764e171
9eb5a7e
7380260
c03aa27
da62de1
12e22e1
f8f5da5
66a1347
870805f
64ec990
ccec195
90385f0
6d0d300
c6488a5
2317d42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package flags | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/pokt-network/pocket/runtime/configs" | ||
) | ||
|
||
var Cfg *configs.Config | ||
|
||
func ParseConfigAndFlags(_ *cobra.Command, _ []string) error { | ||
// by this time, the config path should be set | ||
Cfg = configs.ParseConfig(ConfigPath) | ||
|
||
// set final `remote_cli_url` value; order of precedence: flag > env var > config > default | ||
RemoteCLIURL = viper.GetString("remote_cli_url") | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package peer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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" | ||
) | ||
|
||
var ErrRouterType = fmt.Errorf("must specify one of --staked, --unstaked, or --all") | ||
|
||
func NewListCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "List", | ||
Short: "List the known peers", | ||
Long: "Prints a table of the Peer ID, Pokt Address and Service URL of the known peers", | ||
Aliases: []string{"list", "ls"}, | ||
RunE: listRunE, | ||
} | ||
} | ||
|
||
func listRunE(cmd *cobra.Command, _ []string) error { | ||
var routerType debug.RouterType | ||
|
||
bus, err := helpers.GetBusFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch { | ||
case stakedFlag && !unstakedFlag && !allFlag: | ||
routerType = debug.StakedRouterType | ||
case unstakedFlag && !stakedFlag && !allFlag: | ||
routerType = debug.UnstakedRouterType | ||
case stakedFlag || unstakedFlag: | ||
return ErrRouterType | ||
// even if `allFlag` is false, we still want to print all connections | ||
default: | ||
routerType = debug.AllRouterTypes | ||
} | ||
|
||
debugMsg := &messaging.DebugMessage{ | ||
Action: messaging.DebugMessageAction_DEBUG_P2P_PRINT_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("error creating anypb from debug message: %w", err) | ||
} | ||
|
||
if localFlag { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comments in #801. They're similar to this as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Olshansk can you clarify which comment you are referring to? And what you think should change here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E.g. #801 (comment) @h5law Can you take a stab at #801 first? I (accidently) reviewed it before this one so I avoid repeating some of the stylistic recommendations here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. This one is on me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have gone through #801 and I think I have already addressed most of the relavent comments in this PR as is |
||
if err := debug.PrintPeerList(bus, routerType); err != nil { | ||
return fmt.Errorf("error printing peer list: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// TECHDEBT(#811): will need to wait for DHT bootstrapping to complete before | ||
// p2p broadcast can be used with to reach unstaked actors. | ||
// CONSIDERATION: add the peer commands to the interactive CLI as the P2P module | ||
// instance could persist between commands. Other interactive CLI commands which | ||
// rely on unstaked actor router broadcast are working as expected. | ||
|
||
// TECHDEBT(#811): use broadcast instead to reach all peers. | ||
return sendToStakedPeers(cmd, debugMsgAny) | ||
} | ||
|
||
func sendToStakedPeers(cmd *cobra.Command, debugMsgAny *anypb.Any) error { | ||
bus, err := helpers.GetBusFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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") | ||
} | ||
|
||
for _, peer := range pstore.GetPeerList() { | ||
if err := bus.GetP2PModule().Send(peer.GetAddress(), debugMsgAny); err != nil { | ||
logger.Global.Error().Err(err).Msg("failed to send debug message") | ||
} | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package peer | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/pokt-network/pocket/app/client/cli/helpers" | ||
) | ||
|
||
var allFlag, | ||
stakedFlag, | ||
unstakedFlag, | ||
localFlag bool | ||
|
||
func NewPeerCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "Peer", | ||
Short: "Manage peers", | ||
Aliases: []string{"peer"}, | ||
PersistentPreRunE: helpers.P2PDependenciesPreRunE, | ||
} | ||
|
||
cmd.PersistentFlags(). | ||
BoolVarP( | ||
&allFlag, | ||
"all", "a", | ||
false, | ||
"operations apply to both staked & unstaked router peerstores (default)", | ||
) | ||
cmd.PersistentFlags(). | ||
BoolVarP( | ||
&stakedFlag, | ||
"staked", "s", | ||
false, | ||
"operations only apply to staked router peerstore (i.e. raintree)", | ||
) | ||
cmd.PersistentFlags(). | ||
BoolVarP( | ||
&unstakedFlag, | ||
"unstaked", "u", | ||
false, | ||
"operations only apply to unstaked (including staked as a subset) router peerstore (i.e. gossipsub)", | ||
) | ||
cmd.PersistentFlags(). | ||
BoolVarP( | ||
&localFlag, | ||
"local", "l", | ||
false, | ||
"operations apply to the local (CLI binary's) P2P module instead of being broadcast", | ||
) | ||
|
||
// Add subcommands | ||
cmd.AddCommand(NewListCommand()) | ||
|
||
return cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we do this for the other
flags.go
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intended to revert this change. This is the direction I started when investigating #891 (comment), I thought I had reverted it but apparently failed.
To answer your question, the reason for this is because viper's only integration with flags is to support setting a viper key based on the flag, but not the other way around. I.e: Viper won't update the flag value. This only applies to bound flags
This means that we have to do one of the following consistently for bound flags:
viper.GetString("<flag key>")
(or a helper containing it) anywhere we need the valueI opted for the latter option as I felt it was more conventional and easier to read and maintain.