2024-05-01 04:26:39 +00:00
|
|
|
package main
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/bytes"
|
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2023-04-04 00:08:45 +00:00
|
|
|
coretypes "github.com/tendermint/tendermint/rpc/core/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidatorInfo is info about the node's validator, same as Tendermint,
|
|
|
|
// except that we use our own PubKey.
|
|
|
|
type validatorInfo struct {
|
|
|
|
Address bytes.HexBytes `json:"address"`
|
|
|
|
PubKey cryptotypes.PubKey `json:"pub_key"`
|
|
|
|
VotingPower int64 `json:"voting_power"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResultStatus is node's info, same as Tendermint, except that we use our own
|
|
|
|
// PubKey.
|
|
|
|
type resultStatus struct {
|
|
|
|
NodeInfo p2p.DefaultNodeInfo `json:"node_info"`
|
2023-04-04 00:08:45 +00:00
|
|
|
SyncInfo coretypes.SyncInfo `json:"sync_info"`
|
2022-03-24 14:46:09 +00:00
|
|
|
ValidatorInfo validatorInfo `json:"validator_info"`
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatusCommand returns the command to return the status of the network.
|
|
|
|
func StatusCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "status",
|
|
|
|
Short: "Query remote node for status",
|
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := getNodeStatus(clientCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// `status` has TM pubkeys, we need to convert them to our pubkeys.
|
|
|
|
pk, err := cryptocodec.FromTmPubKeyInterface(status.ValidatorInfo.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
statusWithPk := resultStatus{
|
|
|
|
NodeInfo: status.NodeInfo,
|
|
|
|
SyncInfo: status.SyncInfo,
|
|
|
|
ValidatorInfo: validatorInfo{
|
|
|
|
Address: status.ValidatorInfo.Address,
|
|
|
|
PubKey: pk,
|
|
|
|
VotingPower: status.ValidatorInfo.VotingPower,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := clientCtx.LegacyAmino.MarshalJSON(statusWithPk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println(string(output))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
func getNodeStatus(clientCtx client.Context) (*coretypes.ResultStatus, error) {
|
2022-01-08 00:39:27 +00:00
|
|
|
node, err := clientCtx.GetNode()
|
|
|
|
if err != nil {
|
2023-04-04 00:08:45 +00:00
|
|
|
return &coretypes.ResultStatus{}, err
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node.Status(context.Background())
|
|
|
|
}
|