client: rename CliContext to Context (#6290)

* Refactor CliContext as Context

* Fix lint issues

* Fix goimports

* Fix gov tests

* Resolved ci-lint issues

* Add changelog

* Rename cliCtx to clientCtx

* Fix mocks and routes

* Add changelog

* Update changelog

* Apply suggestions from code review

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* merge client/rpc/ro{ot,utes}.go

* Update docs

* client/rpc: remove redundant client/rpc.RegisterRPCRoutes

* regenerate mocks

* Update ADRs

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
SaReN
2020-06-01 12:46:03 +00:00
committed by GitHub
co-authored by Alessio Treglia Federico Kunze mergify[bot]
parent 654b2fdd10
commit 39f53ac22f
158 changed files with 1810 additions and 1830 deletions
+16 -16
View File
@@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"
@@ -34,9 +34,9 @@ func BlockCommand() *cobra.Command {
return cmd
}
func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
func getBlock(clientCtx client.Context, height *int64) ([]byte, error) {
// get the node
node, err := cliCtx.GetNode()
node, err := clientCtx.GetNode()
if err != nil {
return nil, err
}
@@ -49,8 +49,8 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
return nil, err
}
if !cliCtx.TrustNode {
check, err := cliCtx.Verify(res.Block.Height)
if !clientCtx.TrustNode {
check, err := clientCtx.Verify(res.Block.Height)
if err != nil {
return nil, err
}
@@ -64,7 +64,7 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
}
}
if cliCtx.Indent {
if clientCtx.Indent {
return codec.Cdc.MarshalJSONIndent(res, "", " ")
}
@@ -72,8 +72,8 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
}
// get the current blockchain height
func GetChainHeight(cliCtx context.CLIContext) (int64, error) {
node, err := cliCtx.GetNode()
func GetChainHeight(clientCtx client.Context) (int64, error) {
node, err := clientCtx.GetNode()
if err != nil {
return -1, err
}
@@ -103,7 +103,7 @@ func printBlock(cmd *cobra.Command, args []string) error {
}
}
output, err := getBlock(context.NewCLIContext(), height)
output, err := getBlock(client.NewContext(), height)
if err != nil {
return err
}
@@ -115,7 +115,7 @@ func printBlock(cmd *cobra.Command, args []string) error {
// REST
// REST handler to get a block
func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func BlockRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@@ -126,7 +126,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
chainHeight, err := GetChainHeight(cliCtx)
chainHeight, err := GetChainHeight(clientCtx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height")
return
@@ -137,23 +137,23 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
output, err := getBlock(cliCtx, &height)
output, err := getBlock(clientCtx, &height)
if rest.CheckInternalServerError(w, err) {
return
}
rest.PostProcessResponseBare(w, cliCtx, output)
rest.PostProcessResponseBare(w, clientCtx, output)
}
}
// REST handler to get the latest block
func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func LatestBlockRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
output, err := getBlock(cliCtx, nil)
output, err := getBlock(clientCtx, nil)
if rest.CheckInternalServerError(w, err) {
return
}
rest.PostProcessResponseBare(w, cliCtx, output)
rest.PostProcessResponseBare(w, clientCtx, output)
}
}
-17
View File
@@ -1,17 +0,0 @@
package rpc
import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
)
// Register REST endpoints
func RegisterRPCRoutes(cliCtx context.CLIContext, r *mux.Router) {
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")
}
+17
View File
@@ -0,0 +1,17 @@
package rpc
import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
)
// Register REST endpoints.
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/node_info", NodeInfoRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/syncing", NodeSyncingRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/blocks/latest", LatestBlockRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/blocks/{height}", BlockRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandlerFn(clientCtx)).Methods("GET")
}
+12 -12
View File
@@ -9,7 +9,7 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"
@@ -32,8 +32,8 @@ func StatusCommand() *cobra.Command {
return cmd
}
func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
node, err := cliCtx.GetNode()
func getNodeStatus(clientCtx client.Context) (*ctypes.ResultStatus, error) {
node, err := clientCtx.GetNode()
if err != nil {
return &ctypes.ResultStatus{}, err
}
@@ -47,14 +47,14 @@ func printNodeStatus(_ *cobra.Command, _ []string) error {
// No need to verify proof in getting node status
viper.Set(flags.FlagKeyringBackend, flags.DefaultKeyringBackend)
cliCtx := context.NewCLIContext()
status, err := getNodeStatus(cliCtx)
clientCtx := client.NewContext()
status, err := getNodeStatus(clientCtx)
if err != nil {
return err
}
var output []byte
if cliCtx.Indent {
if clientCtx.Indent {
output, err = codec.Cdc.MarshalJSONIndent(status, "", " ")
} else {
output, err = codec.Cdc.MarshalJSON(status)
@@ -76,9 +76,9 @@ type NodeInfoResponse struct {
}
// REST handler for node info
func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func NodeInfoRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus(cliCtx)
status, err := getNodeStatus(clientCtx)
if rest.CheckInternalServerError(w, err) {
return
}
@@ -87,7 +87,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
DefaultNodeInfo: status.NodeInfo,
ApplicationVersion: version.NewInfo(),
}
rest.PostProcessResponseBare(w, cliCtx, resp)
rest.PostProcessResponseBare(w, clientCtx, resp)
}
}
@@ -97,13 +97,13 @@ type SyncingResponse struct {
}
// REST handler for node syncing
func NodeSyncingRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func NodeSyncingRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus(cliCtx)
status, err := getNodeStatus(clientCtx)
if rest.CheckInternalServerError(w, err) {
return
}
rest.PostProcessResponseBare(w, cliCtx, SyncingResponse{Syncing: status.SyncInfo.CatchingUp})
rest.PostProcessResponseBare(w, clientCtx, SyncingResponse{Syncing: status.SyncInfo.CatchingUp})
}
}
+15 -15
View File
@@ -13,7 +13,7 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -43,14 +43,14 @@ func ValidatorCommand(cdc *codec.Codec) *cobra.Command {
}
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
result, err := GetValidators(cliCtx, height, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))
result, err := GetValidators(clientCtx, height, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))
if err != nil {
return err
}
return cliCtx.PrintOutput(result)
return clientCtx.PrintOutput(result)
},
}
@@ -119,9 +119,9 @@ func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error
}
// GetValidators from client
func GetValidators(cliCtx context.CLIContext, height *int64, page, limit int) (ResultValidatorsOutput, error) {
func GetValidators(clientCtx client.Context, height *int64, page, limit int) (ResultValidatorsOutput, error) {
// get the node
node, err := cliCtx.GetNode()
node, err := clientCtx.GetNode()
if err != nil {
return ResultValidatorsOutput{}, err
}
@@ -131,8 +131,8 @@ func GetValidators(cliCtx context.CLIContext, height *int64, page, limit int) (R
return ResultValidatorsOutput{}, err
}
if !cliCtx.TrustNode {
check, err := cliCtx.Verify(validatorsRes.BlockHeight)
if !clientCtx.TrustNode {
check, err := clientCtx.Verify(validatorsRes.BlockHeight)
if err != nil {
return ResultValidatorsOutput{}, err
}
@@ -160,7 +160,7 @@ func GetValidators(cliCtx context.CLIContext, height *int64, page, limit int) (R
// REST
// Validator Set at a height REST handler
func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func ValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 100)
if err != nil {
@@ -175,7 +175,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
chainHeight, err := GetChainHeight(cliCtx)
chainHeight, err := GetChainHeight(clientCtx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height")
return
@@ -185,16 +185,16 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
output, err := GetValidators(cliCtx, &height, page, limit)
output, err := GetValidators(clientCtx, &height, page, limit)
if rest.CheckInternalServerError(w, err) {
return
}
rest.PostProcessResponse(w, cliCtx, output)
rest.PostProcessResponse(w, clientCtx, output)
}
}
// Latest Validator Set REST handler
func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
func LatestValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 100)
if err != nil {
@@ -202,11 +202,11 @@ func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerF
return
}
output, err := GetValidators(cliCtx, nil, page, limit)
output, err := GetValidators(clientCtx, nil, page, limit)
if rest.CheckInternalServerError(w, err) {
return
}
rest.PostProcessResponse(w, cliCtx, output)
rest.PostProcessResponse(w, clientCtx, output)
}
}