Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup node_info Endpoint #4837

Merged
merged 12 commits into from
Aug 2, 2019
2 changes: 2 additions & 0 deletions .pending/breaking/rest/_Remove-version-and-
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions client/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion client/lcd/statik/statik.go

Large diffs are not rendered by default.

98 changes: 57 additions & 41 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
moniker:
type: string
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
example: on
rpc_address:
listen_addr:
type: string
example: tcp://0.0.0.0:26657
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:
Expand All @@ -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: bool
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
500:
description: Server internal error
/blocks/latest:
Expand Down
32 changes: 0 additions & 32 deletions client/rpc/root.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,17 @@
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")
r.HandleFunc("/blocks/{height}", BlockRequestHandlerFn(cliCtx)).Methods("GET")
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)
}
}
}
34 changes: 20 additions & 14 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package rpc

import (
"fmt"
"log"
"net/http"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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})
}
}
29 changes: 29 additions & 0 deletions types/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 20 additions & 21 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)
}