cosmos-sdk/client/rpc/status.go

110 lines
2.8 KiB
Go
Raw Normal View History

package rpc
import (
"fmt"
"net/http"
"github.com/spf13/cobra"
2018-12-10 06:27:25 -08:00
"github.com/spf13/viper"
2018-12-10 06:27:25 -08:00
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"
"github.com/tendermint/tendermint/p2p"
)
// 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: printNodeStatus,
}
2018-08-06 11:11:30 -07:00
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
return cmd
}
2018-08-06 11:11:30 -07:00
func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
node, err := cliCtx.GetNode()
2018-02-28 15:26:39 -08:00
if err != nil {
return &ctypes.ResultStatus{}, err
2018-02-28 15:26:39 -08:00
}
2018-08-06 11:11:30 -07:00
return node.Status()
}
func printNodeStatus(_ *cobra.Command, _ []string) error {
// No need to verify proof in getting node status
viper.Set(flags.FlagTrustNode, true)
// No need to verify proof in getting node status
viper.Set(flags.FlagKeyringBackend, flags.DefaultKeyringBackend)
cliCtx := context.NewCLIContext()
status, err := getNodeStatus(cliCtx)
if err != nil {
return err
}
var output []byte
if cliCtx.Indent {
output, err = codec.Cdc.MarshalJSONIndent(status, "", " ")
} else {
output, err = codec.Cdc.MarshalJSON(status)
}
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
// NodeInfoResponse defines a response type that contains node status and version
// information.
type NodeInfoResponse struct {
p2p.DefaultNodeInfo `json:"node_info"`
ApplicationVersion version.Info `json:"application_version"`
}
2018-04-18 21:49:24 -07:00
// REST handler for node info
2018-08-06 11:11:30 -07:00
func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2018-08-06 11:11:30 -07:00
status, err := getNodeStatus(cliCtx)
if rest.CheckInternalServerError(w, err) {
return
}
resp := NodeInfoResponse{
DefaultNodeInfo: status.NodeInfo,
ApplicationVersion: version.NewInfo(),
}
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
2018-03-08 05:39:59 -08:00
// SyncingResponse defines a response type that contains node syncing information.
type SyncingResponse struct {
Syncing bool `json:"syncing"`
}
2018-04-18 21:49:24 -07:00
// REST handler for node syncing
2018-08-06 11:11:30 -07:00
func NodeSyncingRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2018-08-06 11:11:30 -07:00
status, err := getNodeStatus(cliCtx)
if rest.CheckInternalServerError(w, err) {
return
}
2018-03-08 05:39:59 -08:00
rest.PostProcessResponseBare(w, cliCtx, SyncingResponse{Syncing: status.SyncInfo.CatchingUp})
2018-03-08 05:39:59 -08:00
}
}