2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-02-23 10:23:24 -08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/spf13/viper"
|
2019-02-14 08:53:36 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-06-04 03:38:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2019-08-02 08:52:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2018-02-23 10:23:24 -08:00
|
|
|
)
|
|
|
|
|
2019-08-02 08:52:55 -07:00
|
|
|
// StatusCommand returns the command to return the status of the network.
|
2018-11-07 15:03:00 -08:00
|
|
|
func StatusCommand() *cobra.Command {
|
2018-02-23 10:23:24 -08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "status",
|
|
|
|
Short: "Query remote node for status",
|
2018-02-28 23:16:54 -08:00
|
|
|
RunE: printNodeStatus,
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
|
|
|
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
|
2018-02-23 10:23:24 -08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
func getNodeStatus(clientCtx client.Context) (*ctypes.ResultStatus, error) {
|
|
|
|
node, err := clientCtx.GetNode()
|
2018-02-28 15:26:39 -08:00
|
|
|
if err != nil {
|
2018-02-28 23:16:54 -08:00
|
|
|
return &ctypes.ResultStatus{}, err
|
2018-02-28 15:26:39 -08:00
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
return node.Status()
|
|
|
|
}
|
|
|
|
|
2019-08-02 08:52:55 -07:00
|
|
|
func printNodeStatus(_ *cobra.Command, _ []string) error {
|
2018-10-04 04:00:24 -07:00
|
|
|
// No need to verify proof in getting node status
|
2019-05-28 01:44:04 -07:00
|
|
|
viper.Set(flags.FlagTrustNode, true)
|
2020-04-23 12:01:47 -07:00
|
|
|
// No need to verify proof in getting node status
|
|
|
|
viper.Set(flags.FlagKeyringBackend, flags.DefaultKeyringBackend)
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx := client.NewContext()
|
|
|
|
status, err := getNodeStatus(clientCtx)
|
2018-02-23 10:23:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
output, err := legacy.Cdc.MarshalJSON(status)
|
2018-02-23 10:23:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-02-23 10:23:24 -08:00
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
// NodeInfoResponse defines a response type that contains node status and version
|
|
|
|
// information.
|
|
|
|
type NodeInfoResponse struct {
|
2019-08-02 08:52:55 -07:00
|
|
|
p2p.DefaultNodeInfo `json:"node_info"`
|
|
|
|
|
|
|
|
ApplicationVersion version.Info `json:"application_version"`
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// REST handler for node info
|
2020-06-01 05:46:03 -07:00
|
|
|
func NodeInfoRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-06-01 05:46:03 -07:00
|
|
|
status, err := getNodeStatus(clientCtx)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
resp := NodeInfoResponse{
|
2019-08-02 08:52:55 -07:00
|
|
|
DefaultNodeInfo: status.NodeInfo,
|
|
|
|
ApplicationVersion: version.NewInfo(),
|
|
|
|
}
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, resp)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 05:39:59 -08:00
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
// SyncingResponse defines a response type that contains node syncing information.
|
|
|
|
type SyncingResponse struct {
|
2019-08-02 08:52:55 -07:00
|
|
|
Syncing bool `json:"syncing"`
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// REST handler for node syncing
|
2020-06-01 05:46:03 -07:00
|
|
|
func NodeSyncingRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-06-01 05:46:03 -07:00
|
|
|
status, err := getNodeStatus(clientCtx)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2018-03-08 05:39:59 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, SyncingResponse{Syncing: status.SyncInfo.CatchingUp})
|
2018-03-08 05:39:59 -08:00
|
|
|
}
|
|
|
|
}
|