Skip to content

Commit

Permalink
(serverv2/cometbft) Read config from commands & handle FlagNode (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed Jun 12, 2024
1 parent c36edbd commit 8d205af
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions server/v2/cometbft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@ import (
"cosmossdk.io/server/v2/cometbft/client/rpc"
"cosmossdk.io/server/v2/cometbft/flags"
auth "cosmossdk.io/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/client"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
)

func (s *CometBFTServer[T]) rpcClient() (rpc.CometRPC, error) {
func (s *CometBFTServer[T]) rpcClient(cmd *cobra.Command) (rpc.CometRPC, error) {
if s.config.Standalone {
client, err := rpchttp.New(s.config.CmtConfig.RPC.ListenAddress)
client, err := rpchttp.New(client.GetConfigFromCmd(cmd).RPC.ListenAddress)
if err != nil {
return nil, err
}
return client, nil
}

if s.Node == nil || cmd.Flags().Changed(flags.FlagNode) {
rpcURI, err := cmd.Flags().GetString(flags.FlagNode)
if err != nil {
return nil, err
}
if rpcURI != "" {
return rpchttp.New(rpcURI)
}
}

return local.New(s.Node), nil
}

Expand All @@ -46,7 +57,7 @@ func (s *CometBFTServer[T]) StatusCommand() *cobra.Command {
Use: "status",
Short: "Query remote node for status",
RunE: func(cmd *cobra.Command, _ []string) error {
rpcclient, err := s.rpcClient()
rpcclient, err := s.rpcClient(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -80,7 +91,8 @@ func (s *CometBFTServer[T]) ShowNodeIDCmd() *cobra.Command {
Use: "show-node-id",
Short: "Show this node's ID",
RunE: func(cmd *cobra.Command, args []string) error {
nodeKey, err := p2p.LoadNodeKey(s.config.CmtConfig.NodeKeyFile())
cmtConfig := client.GetConfigFromCmd(cmd)
nodeKey, err := p2p.LoadNodeKey(cmtConfig.NodeKeyFile())
if err != nil {
return err
}
Expand All @@ -97,7 +109,7 @@ func (s *CometBFTServer[T]) ShowValidatorCmd() *cobra.Command {
Use: "show-validator",
Short: "Show this node's CometBFT validator info",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := s.config.CmtConfig
cfg := client.GetConfigFromCmd(cmd)
privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
pk, err := privValidator.GetPubKey()
if err != nil {
Expand Down Expand Up @@ -131,7 +143,7 @@ func (s *CometBFTServer[T]) ShowAddressCmd() *cobra.Command {
Use: "show-address",
Short: "Shows this node's CometBFT validator consensus address",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := s.config.CmtConfig
cfg := client.GetConfigFromCmd(cmd)
privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
// TODO: use address codec?
valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
Expand Down Expand Up @@ -188,7 +200,7 @@ for. Each module documents its respective events under 'xx_events.md'.
version.AppName,
),
RunE: func(cmd *cobra.Command, args []string) error {
rpcclient, err := s.rpcClient()
rpcclient, err := s.rpcClient(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -241,7 +253,7 @@ $ %s query block --%s=%s <hash>
RunE: func(cmd *cobra.Command, args []string) error {
typ, _ := cmd.Flags().GetString(auth.FlagType)

rpcclient, err := s.rpcClient()
rpcclient, err := s.rpcClient(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -332,7 +344,7 @@ func (s *CometBFTServer[T]) QueryBlockResultsCmd() *cobra.Command {

// TODO: we should be able to do this without using client context

node, err := s.rpcClient()
node, err := s.rpcClient(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -392,6 +404,7 @@ func (s *CometBFTServer[T]) BootstrapStateCmd() *cobra.Command {
Short: "Bootstrap CometBFT state at an arbitrary block height using a light client",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
height, err := cmd.Flags().GetUint64("height")
if err != nil {
return err
Expand All @@ -404,7 +417,7 @@ func (s *CometBFTServer[T]) BootstrapStateCmd() *cobra.Command {
}

// TODO genensis doc provider and apphash
return node.BootstrapState(cmd.Context(), s.config.CmtConfig, cmtcfg.DefaultDBProvider, nil, height, nil)
return node.BootstrapState(cmd.Context(), cfg, cmtcfg.DefaultDBProvider, nil, height, nil)
},
}

Expand Down

0 comments on commit 8d205af

Please sign in to comment.