2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-03-08 05:39:59 -08:00
|
|
|
"strconv"
|
2018-02-23 10:23:24 -08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-10-04 04:00:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
2018-09-25 14:36:42 -07:00
|
|
|
"github.com/spf13/viper"
|
2018-10-03 08:48:23 -07:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2018-02-23 10:23:24 -08:00
|
|
|
)
|
|
|
|
|
2018-11-07 15:03:00 -08:00
|
|
|
// StatusCommand returns the status of the network
|
|
|
|
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
|
|
|
|
2018-06-13 15:13:51 -07:00
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
2018-09-25 14:36:42 -07:00
|
|
|
viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode))
|
2018-10-04 04:00:24 -07:00
|
|
|
cmd.Flags().Bool(client.FlagIndentResponse, false, "Add indent to JSON response")
|
2018-02-23 10:23:24 -08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
|
2018-02-23 10:23:24 -08:00
|
|
|
// get the node
|
2018-08-06 11:11:30 -07:00
|
|
|
node, err := cliCtx.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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CMD
|
|
|
|
|
|
|
|
func printNodeStatus(cmd *cobra.Command, args []string) error {
|
2018-10-04 04:00:24 -07:00
|
|
|
// No need to verify proof in getting node status
|
|
|
|
viper.Set(client.FlagTrustNode, true)
|
|
|
|
cliCtx := context.NewCLIContext()
|
|
|
|
status, err := getNodeStatus(cliCtx)
|
2018-02-23 10:23:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
var output []byte
|
|
|
|
if cliCtx.Indent {
|
|
|
|
output, err = cdc.MarshalJSONIndent(status, "", " ")
|
|
|
|
} else {
|
|
|
|
output, err = 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
|
|
|
|
|
|
|
// REST
|
|
|
|
|
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 {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-08-06 11:11:30 -07:00
|
|
|
status, err := getNodeStatus(cliCtx)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
nodeInfo := status.NodeInfo
|
2018-10-04 04:00:24 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, nodeInfo, cliCtx.Indent)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 05:39:59 -08:00
|
|
|
|
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 {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-08-06 11:11:30 -07:00
|
|
|
status, err := getNodeStatus(cliCtx)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-03-08 05:39:59 -08:00
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
syncing := status.SyncInfo.CatchingUp
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(strconv.FormatBool(syncing)))
|
2018-03-08 05:39:59 -08:00
|
|
|
}
|
|
|
|
}
|