github.com/Finschia/finschia-sdk@v0.48.1/client/rpc/status.go (about)

     1  package rpc
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/Finschia/ostracon/libs/bytes"
     9  	"github.com/Finschia/ostracon/p2p"
    10  	ctypes "github.com/Finschia/ostracon/rpc/core/types"
    11  
    12  	"github.com/Finschia/finschia-sdk/client"
    13  	"github.com/Finschia/finschia-sdk/client/flags"
    14  	cryptocodec "github.com/Finschia/finschia-sdk/crypto/codec"
    15  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
    16  )
    17  
    18  // ValidatorInfo is info about the node's validator, same as Ostracon,
    19  // except that we use our own PubKey.
    20  type validatorInfo struct {
    21  	Address     bytes.HexBytes
    22  	PubKey      cryptotypes.PubKey
    23  	VotingPower int64
    24  }
    25  
    26  // ResultStatus is node's info, same as Ostracon, except that we use our own
    27  // PubKey.
    28  type resultStatus struct {
    29  	NodeInfo      p2p.DefaultNodeInfo
    30  	SyncInfo      ctypes.SyncInfo
    31  	ValidatorInfo validatorInfo
    32  }
    33  
    34  // StatusCommand returns the command to return the status of the network.
    35  func StatusCommand() *cobra.Command {
    36  	cmd := &cobra.Command{
    37  		Use:   "status",
    38  		Short: "Query remote node for status",
    39  		RunE: func(cmd *cobra.Command, _ []string) error {
    40  			clientCtx, err := client.GetClientQueryContext(cmd)
    41  			if err != nil {
    42  				return err
    43  			}
    44  
    45  			status, err := getNodeStatus(clientCtx)
    46  			if err != nil {
    47  				return err
    48  			}
    49  
    50  			// `status` has OC pubkeys, we need to convert them to our pubkeys.
    51  			pk, err := cryptocodec.FromOcPubKeyInterface(status.ValidatorInfo.PubKey)
    52  			if err != nil {
    53  				return err
    54  			}
    55  			statusWithPk := resultStatus{
    56  				NodeInfo: status.NodeInfo,
    57  				SyncInfo: status.SyncInfo,
    58  				ValidatorInfo: validatorInfo{
    59  					Address:     status.ValidatorInfo.Address,
    60  					PubKey:      pk,
    61  					VotingPower: status.ValidatorInfo.VotingPower,
    62  				},
    63  			}
    64  
    65  			output, err := clientCtx.LegacyAmino.MarshalJSON(statusWithPk)
    66  			if err != nil {
    67  				return err
    68  			}
    69  
    70  			cmd.Println(string(output))
    71  			return nil
    72  		},
    73  	}
    74  
    75  	cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
    76  
    77  	return cmd
    78  }
    79  
    80  func getNodeStatus(clientCtx client.Context) (*ctypes.ResultStatus, error) {
    81  	node, err := clientCtx.GetNode()
    82  	if err != nil {
    83  		return &ctypes.ResultStatus{}, err
    84  	}
    85  
    86  	return node.Status(context.Background())
    87  }