Merge PR #4837: Cleanup node_info Endpoint

This commit is contained in:
Alexander Bezobchuk 2019-08-02 11:52:55 -04:00 committed by GitHub
parent c4e3578087
commit 451535bd79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 134 additions and 116 deletions

View File

@ -0,0 +1,2 @@
[\#4837](https://github.com/cosmos/cosmos-sdk/pull/4837) Remove /version and /node_version
endpoints in favor of refactoring /node_info to also include application version info.

View File

@ -25,11 +25,11 @@ build: go.sum
update-swagger-docs: statik
$(BINDIR)/statik -src=client/lcd/swagger-ui -dest=client/lcd -f -m
if [ -n "$(git status --porcelain)" ]; then \
echo "swagger docs out of sync";\
@if [ -n "$(git status --porcelain)" ]; then \
echo "\033[91mSwagger docs are out of sync!!!\033[0m";\
exit 1;\
else \
echo "swagger docs are in sync";\
echo "\033[92mSwagger docs are in sync\033[0m";\
fi
.PHONY: update-swagger-docs

View File

@ -94,8 +94,6 @@ var (
BlockRequestHandlerFn = rpc.BlockRequestHandlerFn
LatestBlockRequestHandlerFn = rpc.LatestBlockRequestHandlerFn
RegisterRPCRoutes = rpc.RegisterRPCRoutes
CLIVersionRequestHandler = rpc.CLIVersionRequestHandler
NodeVersionRequestHandler = rpc.NodeVersionRequestHandler
StatusCommand = rpc.StatusCommand
NodeInfoRequestHandlerFn = rpc.NodeInfoRequestHandlerFn
NodeSyncingRequestHandlerFn = rpc.NodeSyncingRequestHandlerFn

File diff suppressed because one or more lines are too long

View File

@ -35,15 +35,6 @@ securityDefinitions:
kms:
type: basic
paths:
/version:
get:
summary: Version of Gaia-lite
tags:
- Misc
description: Get the version of gaia-lite running locally to compare against expected
responses:
200:
description: Plaintext version i.e. "v0.25.0"
/node_info:
get:
description: Information about the connected node
@ -58,44 +49,62 @@ paths:
schema:
type: object
properties:
id:
type: string
moniker:
type: string
example: validator-name
protocol_version:
application_version:
properties:
p2p:
build_tags:
type: string
example: 7
block:
client_name:
type: string
example: 10
app:
commit:
type: string
example: 0
network:
type: string
example: gaia-2
channels:
type: string
listen_addr:
type: string
example: 192.168.56.1:26656
version:
description: Tendermint version
type: string
example: 0.15.0
other:
description: more information on versions
type: object
go:
type: string
name:
type: string
server_name:
type: string
version:
type: string
node_info:
properties:
tx_index:
id:
type: string
example: on
rpc_address:
moniker:
type: string
example: tcp://0.0.0.0:26657
example: validator-name
protocol_version:
properties:
p2p:
type: string
example: 7
block:
type: string
example: 10
app:
type: string
example: 0
network:
type: string
example: gaia-2
channels:
type: string
listen_addr:
type: string
example: 192.168.56.1:26656
version:
description: Tendermint version
type: string
example: 0.15.0
other:
description: more information on versions
type: object
properties:
tx_index:
type: string
example: on
rpc_address:
type: string
example: tcp://0.0.0.0:26657
500:
description: Failed to query node status
/syncing:
@ -104,9 +113,16 @@ paths:
tags:
- Tendermint RPC
description: Get if the node is currently syning with other nodes
produces:
- application/json
responses:
200:
description: '"true" or "false"'
description: Node syncing status
schema:
type: object
properties:
syncing:
type: boolean
500:
description: Server internal error
/blocks/latest:
@ -2481,4 +2497,4 @@ definitions:
total:
type: array
items:
$ref: "#/definitions/Coin"
$ref: "#/definitions/Coin"

View File

@ -1,21 +1,13 @@
package rpc
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"
)
// Register REST endpoints
func RegisterRPCRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/version", CLIVersionRequestHandler).Methods("GET")
r.HandleFunc("/node_version", NodeVersionRequestHandler(cliCtx)).Methods("GET")
r.HandleFunc("/node_info", NodeInfoRequestHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/syncing", NodeSyncingRequestHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/blocks/latest", LatestBlockRequestHandlerFn(cliCtx)).Methods("GET")
@ -23,27 +15,3 @@ func RegisterRPCRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandlerFn(cliCtx)).Methods("GET")
}
// cli version REST handler endpoint
func CLIVersionRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte(fmt.Sprintf("{\"version\": \"%s\"}", version.Version))); err != nil {
log.Printf("could not write response: %v", err)
}
}
// connected node version REST handler endpoint
func NodeVersionRequestHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, _, err := cliCtx.Query("/app/version")
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(res); err != nil {
log.Printf("could not write response: %v", err)
}
}
}

View File

@ -2,9 +2,7 @@ package rpc
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -15,9 +13,12 @@ import (
"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 status of the network
// StatusCommand returns the command to return the status of the network.
func StatusCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
@ -32,7 +33,6 @@ func StatusCommand() *cobra.Command {
}
func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
// get the node
node, err := cliCtx.GetNode()
if err != nil {
return &ctypes.ResultStatus{}, err
@ -41,9 +41,7 @@ func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
return node.Status()
}
// CMD
func printNodeStatus(cmd *cobra.Command, args []string) error {
func printNodeStatus(_ *cobra.Command, _ []string) error {
// No need to verify proof in getting node status
viper.Set(flags.FlagTrustNode, true)
cliCtx := context.NewCLIContext()
@ -66,7 +64,11 @@ func printNodeStatus(cmd *cobra.Command, args []string) error {
return nil
}
// REST
type nodeInfoResponse struct {
p2p.DefaultNodeInfo `json:"node_info"`
ApplicationVersion version.Info `json:"application_version"`
}
// REST handler for node info
func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
@ -77,11 +79,18 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
nodeInfo := status.NodeInfo
rest.PostProcessResponse(w, cliCtx, nodeInfo)
resp := nodeInfoResponse{
DefaultNodeInfo: status.NodeInfo,
ApplicationVersion: version.NewInfo(),
}
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
type syncingResponse struct {
Syncing bool `json:"syncing"`
}
// REST handler for node syncing
func NodeSyncingRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@ -91,9 +100,6 @@ func NodeSyncingRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
syncing := status.SyncInfo.CatchingUp
if _, err := w.Write([]byte(strconv.FormatBool(syncing))); err != nil {
log.Printf("could not write response: %v", err)
}
rest.PostProcessResponseBare(w, cliCtx, syncingResponse{Syncing: status.SyncInfo.CatchingUp})
}
}

View File

@ -237,6 +237,35 @@ func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, cliCtx context.CL
return cliCtx, true
}
// PostProcessResponseBare post processes a body similar to PostProcessResponse
// except it does not wrap the body and inject the height.
func PostProcessResponseBare(w http.ResponseWriter, cliCtx context.CLIContext, body interface{}) {
var (
resp []byte
err error
)
switch body.(type) {
case []byte:
resp = body.([]byte)
default:
if cliCtx.Indent {
resp, err = cliCtx.Codec.MarshalJSONIndent(body, "", " ")
} else {
resp, err = cliCtx.Codec.MarshalJSON(body)
}
if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(resp)
}
// PostProcessResponse performs post processing for a REST response. The result
// returned to clients will contain two fields, the height at which the resource
// was queried at and the original result.

View File

@ -22,7 +22,7 @@ var Cmd = &cobra.Command{
Use: "version",
Short: "Print the app version",
RunE: func(_ *cobra.Command, _ []string) error {
verInfo := newVersionInfo()
verInfo := NewInfo()
if !viper.GetBool(flagLong) {
fmt.Println(verInfo.Version)

View File

@ -33,33 +33,23 @@ var (
Version = ""
// commit
Commit = ""
// hash of the go.sum file
GoSumHash = ""
// build tags
BuildTags = ""
)
type versionInfo struct {
Name string `json:"name"`
ServerName string `json:"server_name"`
ClientName string `json:"client_name"`
Version string `json:"version"`
GitCommit string `json:"commit"`
BuildTags string `json:"build_tags"`
GoVersion string `json:"go"`
// Info defines the application version information.
type Info struct {
Name string `json:"name" yaml:"name"`
ServerName string `json:"server_name" yaml:"server_name"`
ClientName string `json:"client_name" yaml:"client_name"`
Version string `json:"version" yaml:"version"`
GitCommit string `json:"commit" yaml:"commit"`
BuildTags string `json:"build_tags" yaml:"build_tags"`
GoVersion string `json:"go" yaml:"go"`
}
func (v versionInfo) String() string {
return fmt.Sprintf(`%s: %s
git commit: %s
build tags: %s
%s`,
v.Name, v.Version, v.GitCommit, v.BuildTags, v.GoVersion,
)
}
func newVersionInfo() versionInfo {
return versionInfo{
func NewInfo() Info {
return Info{
Name: Name,
ServerName: ServerName,
ClientName: ClientName,
@ -69,3 +59,12 @@ func newVersionInfo() versionInfo {
GoVersion: fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH),
}
}
func (vi Info) String() string {
return fmt.Sprintf(`%s: %s
git commit: %s
build tags: %s
%s`,
vi.Name, vi.Version, vi.GitCommit, vi.BuildTags, vi.GoVersion,
)
}