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

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