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
+6 -6
View File
@@ -35,7 +35,7 @@ import (
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -51,8 +51,8 @@ type AppModuleBasic interface {
ValidateGenesis(codec.JSONMarshaler, json.RawMessage) error
// client functionality
RegisterRESTRoutes(context.CLIContext, *mux.Router)
GetTxCmd(context.CLIContext) *cobra.Command
RegisterRESTRoutes(client.Context, *mux.Router)
GetTxCmd(client.Context) *cobra.Command
GetQueryCmd(*codec.Codec) *cobra.Command
}
@@ -97,14 +97,14 @@ func (bm BasicManager) ValidateGenesis(cdc codec.JSONMarshaler, genesis map[stri
}
// RegisterRESTRoutes registers all module rest routes
func (bm BasicManager) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
func (bm BasicManager) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) {
for _, b := range bm {
b.RegisterRESTRoutes(ctx, rtr)
b.RegisterRESTRoutes(clientCtx, rtr)
}
}
// AddTxCommands adds all tx commands to the rootTxCmd
func (bm BasicManager) AddTxCommands(rootTxCmd *cobra.Command, ctx context.CLIContext) {
func (bm BasicManager) AddTxCommands(rootTxCmd *cobra.Command, ctx client.Context) {
for _, b := range bm {
if cmd := b.GetTxCmd(ctx); cmd != nil {
rootTxCmd.AddCommand(cmd)
+7 -7
View File
@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/tests/mocks"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -24,8 +24,8 @@ func TestBasicManager(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
cdc := codec.New()
ctx := context.CLIContext{}
ctx = ctx.WithCodec(cdc)
clientCtx := client.Context{}
clientCtx = clientCtx.WithCodec(cdc)
wantDefaultGenesis := map[string]json.RawMessage{"mockAppModuleBasic1": json.RawMessage(``)}
mockAppModuleBasic1 := mocks.NewMockAppModuleBasic(mockCtrl)
@@ -33,9 +33,9 @@ func TestBasicManager(t *testing.T) {
mockAppModuleBasic1.EXPECT().Name().AnyTimes().Return("mockAppModuleBasic1")
mockAppModuleBasic1.EXPECT().DefaultGenesis(gomock.Eq(cdc)).Times(1).Return(json.RawMessage(``))
mockAppModuleBasic1.EXPECT().ValidateGenesis(gomock.Eq(cdc), gomock.Eq(wantDefaultGenesis["mockAppModuleBasic1"])).Times(1).Return(errFoo)
mockAppModuleBasic1.EXPECT().RegisterRESTRoutes(gomock.Eq(context.CLIContext{}), gomock.Eq(&mux.Router{})).Times(1)
mockAppModuleBasic1.EXPECT().RegisterRESTRoutes(gomock.Eq(client.Context{}), gomock.Eq(&mux.Router{})).Times(1)
mockAppModuleBasic1.EXPECT().RegisterCodec(gomock.Eq(cdc)).Times(1)
mockAppModuleBasic1.EXPECT().GetTxCmd(ctx).Times(1).Return(nil)
mockAppModuleBasic1.EXPECT().GetTxCmd(clientCtx).Times(1).Return(nil)
mockAppModuleBasic1.EXPECT().GetQueryCmd(cdc).Times(1).Return(nil)
mm := module.NewBasicManager(mockAppModuleBasic1)
@@ -50,10 +50,10 @@ func TestBasicManager(t *testing.T) {
require.True(t, errors.Is(errFoo, mm.ValidateGenesis(cdc, wantDefaultGenesis)))
mm.RegisterRESTRoutes(context.CLIContext{}, &mux.Router{})
mm.RegisterRESTRoutes(client.Context{}, &mux.Router{})
mockCmd := &cobra.Command{Use: "root"}
mm.AddTxCommands(mockCmd, ctx)
mm.AddTxCommands(mockCmd, clientCtx)
mm.AddQueryCommands(mockCmd, cdc)
+9 -9
View File
@@ -14,7 +14,7 @@ import (
"github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -229,32 +229,32 @@ func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEm
// ParseQueryHeightOrReturnBadRequest sets the height to execute a query if set by the http request.
// It returns false if there was an error parsing the height.
func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, cliCtx context.CLIContext, r *http.Request) (context.CLIContext, bool) {
func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, clientCtx client.Context, r *http.Request) (client.Context, bool) {
heightStr := r.FormValue("height")
if heightStr != "" {
height, err := strconv.ParseInt(heightStr, 10, 64)
if CheckBadRequestError(w, err) {
return cliCtx, false
return clientCtx, false
}
if height < 0 {
WriteErrorResponse(w, http.StatusBadRequest, "height must be equal or greater than zero")
return cliCtx, false
return clientCtx, false
}
if height > 0 {
cliCtx = cliCtx.WithHeight(height)
clientCtx = clientCtx.WithHeight(height)
}
} else {
cliCtx = cliCtx.WithHeight(0)
clientCtx = clientCtx.WithHeight(0)
}
return cliCtx, true
return clientCtx, 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, ctx context.CLIContext, body interface{}) {
func PostProcessResponseBare(w http.ResponseWriter, ctx client.Context, body interface{}) {
var (
resp []byte
err error
@@ -293,7 +293,7 @@ func PostProcessResponseBare(w http.ResponseWriter, ctx context.CLIContext, body
// 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.
func PostProcessResponse(w http.ResponseWriter, ctx context.CLIContext, resp interface{}) {
func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interface{}) {
var (
result []byte
err error
+19 -19
View File
@@ -15,7 +15,7 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
"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"
@@ -147,25 +147,25 @@ func TestParseQueryHeight(t *testing.T) {
name string
req *http.Request
w http.ResponseWriter
cliCtx context.CLIContext
clientCtx client.Context
expectedHeight int64
expectedOk bool
}{
{"no height", req0, httptest.NewRecorder(), context.CLIContext{}, emptyHeight, true},
{"height", req1, httptest.NewRecorder(), context.CLIContext{}, height, true},
{"invalid height", req2, httptest.NewRecorder(), context.CLIContext{}, emptyHeight, false},
{"negative height", req3, httptest.NewRecorder(), context.CLIContext{}, emptyHeight, false},
{"no height", req0, httptest.NewRecorder(), client.Context{}, emptyHeight, true},
{"height", req1, httptest.NewRecorder(), client.Context{}, height, true},
{"invalid height", req2, httptest.NewRecorder(), client.Context{}, emptyHeight, false},
{"negative height", req3, httptest.NewRecorder(), client.Context{}, emptyHeight, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(tt.w, tt.cliCtx, tt.req)
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(tt.w, tt.clientCtx, tt.req)
if tt.expectedOk {
require.True(t, ok)
require.Equal(t, tt.expectedHeight, cliCtx.Height)
require.Equal(t, tt.expectedHeight, clientCtx.Height)
} else {
require.False(t, ok)
require.Empty(t, tt.expectedHeight, cliCtx.Height)
require.Empty(t, tt.expectedHeight, clientCtx.Height)
}
})
}
@@ -187,7 +187,7 @@ func TestProcessPostResponse(t *testing.T) {
// setup
viper.Set(flags.FlagOffline, true)
ctx := context.NewCLIContext()
ctx := client.NewContext()
height := int64(194423)
privKey := secp256k1.GenPrivKey()
@@ -312,11 +312,11 @@ func TestPostProcessResponseBare(t *testing.T) {
t.Parallel()
// write bytes
ctx := context.CLIContext{}
clientCtx := client.Context{}
w := httptest.NewRecorder()
bs := []byte("text string")
rest.PostProcessResponseBare(w, ctx, bs)
rest.PostProcessResponseBare(w, clientCtx, bs)
res := w.Result() //nolint:bodyclose
require.Equal(t, http.StatusOK, res.StatusCode)
@@ -328,14 +328,14 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, "text string", string(got))
// write struct and indent response
ctx = context.CLIContext{Indent: true}.WithCodec(codec.New())
clientCtx = client.Context{Indent: true}.WithCodec(codec.New())
w = httptest.NewRecorder()
data := struct {
X int `json:"x"`
S string `json:"s"`
}{X: 10, S: "test"}
rest.PostProcessResponseBare(w, ctx, data)
rest.PostProcessResponseBare(w, clientCtx, data)
res = w.Result() //nolint:bodyclose
require.Equal(t, http.StatusOK, res.StatusCode)
@@ -350,14 +350,14 @@ func TestPostProcessResponseBare(t *testing.T) {
}`, string(got))
// write struct, don't indent response
ctx = context.CLIContext{Indent: false}.WithCodec(codec.New())
clientCtx = client.Context{Indent: false}.WithCodec(codec.New())
w = httptest.NewRecorder()
data = struct {
X int `json:"x"`
S string `json:"s"`
}{X: 10, S: "test"}
rest.PostProcessResponseBare(w, ctx, data)
rest.PostProcessResponseBare(w, clientCtx, data)
res = w.Result() //nolint:bodyclose
require.Equal(t, http.StatusOK, res.StatusCode)
@@ -369,11 +369,11 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, `{"x":"10","s":"test"}`, string(got))
// test marshalling failure
ctx = context.CLIContext{Indent: false}.WithCodec(codec.New())
clientCtx = client.Context{Indent: false}.WithCodec(codec.New())
w = httptest.NewRecorder()
data2 := badJSONMarshaller{}
rest.PostProcessResponseBare(w, ctx, data2)
rest.PostProcessResponseBare(w, clientCtx, data2)
res = w.Result() //nolint:bodyclose
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
@@ -395,7 +395,7 @@ func (badJSONMarshaller) MarshalJSON() ([]byte, error) {
// asserts that ResponseRecorder returns the expected code and body
// runs PostProcessResponse on the objects regular interface and on
// the marshalled struct.
func runPostProcessResponse(t *testing.T, ctx context.CLIContext, obj interface{}, expectedBody []byte, indent bool) {
func runPostProcessResponse(t *testing.T, ctx client.Context, obj interface{}, expectedBody []byte, indent bool) {
if indent {
ctx.Indent = indent
}