refactor(client): add client/Context.Codec and deprecate JSONCodec (#9498)
* chore(types): add sdk.Context.Codec and deprecate JSONCodec * Use clientContext.Codec rather than JSONCodec everywhere * update tests to use clientContext.Codec * added a note that EncodingConfig.Marshaler will be renamed to Codec * update changelog * fix tests to use clientCtx.WithCodec instead of WithJSONCodec * fix genutil build * Update simapp/params/encoding.go Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
This commit is contained in:
+20
-6
@@ -22,10 +22,12 @@ import (
|
||||
// Context implements a typical context created in SDK modules for transaction
|
||||
// handling and queries.
|
||||
type Context struct {
|
||||
FromAddress sdk.AccAddress
|
||||
Client rpcclient.Client
|
||||
ChainID string
|
||||
FromAddress sdk.AccAddress
|
||||
Client rpcclient.Client
|
||||
ChainID string
|
||||
// Deprecated: Codec codec will be changed to Codec: codec.Codec
|
||||
JSONCodec codec.JSONCodec
|
||||
Codec codec.Codec
|
||||
InterfaceRegistry codectypes.InterfaceRegistry
|
||||
Input io.Reader
|
||||
Keyring keyring.Keyring
|
||||
@@ -72,9 +74,21 @@ func (ctx Context) WithInput(r io.Reader) Context {
|
||||
return ctx
|
||||
}
|
||||
|
||||
// WithJSONCodec returns a copy of the Context with an updated JSONCodec.
|
||||
// Deprecated: WithJSONCodec returns a copy of the Context with an updated JSONCodec.
|
||||
func (ctx Context) WithJSONCodec(m codec.JSONCodec) Context {
|
||||
ctx.JSONCodec = m
|
||||
// since we are using ctx.Codec everywhere in the SDK, for backward compatibility
|
||||
// we need to try to set it here as well.
|
||||
if c, ok := m.(codec.Codec); ok {
|
||||
ctx.Codec = c
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// WithCodec returns a copy of the Context with an updated Codec.
|
||||
func (ctx Context) WithCodec(m codec.Codec) Context {
|
||||
ctx.JSONCodec = m
|
||||
ctx.Codec = m
|
||||
return ctx
|
||||
}
|
||||
|
||||
@@ -254,10 +268,10 @@ func (ctx Context) PrintBytes(o []byte) error {
|
||||
|
||||
// PrintProto outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
|
||||
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
|
||||
// will be JSON encoded using ctx.JSONCodec. An error is returned upon failure.
|
||||
// will be JSON encoded using ctx.Codec. An error is returned upon failure.
|
||||
func (ctx Context) PrintProto(toPrint proto.Message) error {
|
||||
// always serialize JSON initially because proto json can't be directly YAML encoded
|
||||
out, err := ctx.JSONCodec.MarshalJSON(toPrint)
|
||||
out, err := ctx.Codec.MarshalJSON(toPrint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestContext_PrintObject(t *testing.T) {
|
||||
// proto
|
||||
//
|
||||
registry := testdata.NewTestInterfaceRegistry()
|
||||
ctx = ctx.WithJSONCodec(codec.NewProtoCodec(registry))
|
||||
ctx = ctx.WithCodec(codec.NewProtoCodec(registry))
|
||||
|
||||
// json
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
@@ -32,7 +32,7 @@ func Cmd() *cobra.Command {
|
||||
// getPubKeyFromString decodes SDK PubKey using JSON marshaler.
|
||||
func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) {
|
||||
var pk cryptotypes.PubKey
|
||||
err := ctx.JSONCodec.UnmarshalInterfaceJSON([]byte(pkstr), &pk)
|
||||
err := ctx.Codec.UnmarshalInterfaceJSON([]byte(pkstr), &pk)
|
||||
return pk, err
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ func (s IntegrationTestSuite) TestQueryNodeInfo() {
|
||||
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/node_info", val.APIAddress))
|
||||
s.Require().NoError(err)
|
||||
var getInfoRes tmservice.GetNodeInfoResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(restRes, &getInfoRes))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &getInfoRes))
|
||||
s.Require().Equal(getInfoRes.ApplicationVersion.AppName, version.NewInfo().AppName)
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ func (s IntegrationTestSuite) TestQuerySyncing() {
|
||||
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/syncing", val.APIAddress))
|
||||
s.Require().NoError(err)
|
||||
var syncingRes tmservice.GetSyncingResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(restRes, &syncingRes))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &syncingRes))
|
||||
}
|
||||
|
||||
func (s IntegrationTestSuite) TestQueryLatestBlock() {
|
||||
@@ -82,7 +82,7 @@ func (s IntegrationTestSuite) TestQueryLatestBlock() {
|
||||
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/latest", val.APIAddress))
|
||||
s.Require().NoError(err)
|
||||
var blockInfoRes tmservice.GetLatestBlockResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(restRes, &blockInfoRes))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &blockInfoRes))
|
||||
}
|
||||
|
||||
func (s IntegrationTestSuite) TestQueryBlockByHeight() {
|
||||
@@ -93,7 +93,7 @@ func (s IntegrationTestSuite) TestQueryBlockByHeight() {
|
||||
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/%d", val.APIAddress, 1))
|
||||
s.Require().NoError(err)
|
||||
var blockInfoRes tmservice.GetBlockByHeightResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(restRes, &blockInfoRes))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &blockInfoRes))
|
||||
}
|
||||
|
||||
func (s IntegrationTestSuite) TestQueryLatestValidatorSet() {
|
||||
@@ -124,7 +124,7 @@ func (s IntegrationTestSuite) TestQueryLatestValidatorSet() {
|
||||
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=%d&pagination.limit=%d", val.APIAddress, 0, 1))
|
||||
s.Require().NoError(err)
|
||||
var validatorSetRes tmservice.GetLatestValidatorSetResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(restRes, &validatorSetRes))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &validatorSetRes))
|
||||
s.Require().Equal(1, len(validatorSetRes.Validators))
|
||||
anyPub, err := codectypes.NewAnyWithValue(val.PubKey)
|
||||
s.Require().NoError(err)
|
||||
@@ -183,7 +183,7 @@ func (s IntegrationTestSuite) TestLatestValidatorSet_GRPCGateway() {
|
||||
s.Require().Contains(string(res), tc.expErrMsg)
|
||||
} else {
|
||||
var result tmservice.GetLatestValidatorSetResponse
|
||||
err = vals[0].ClientCtx.JSONCodec.UnmarshalJSON(res, &result)
|
||||
err = vals[0].ClientCtx.Codec.UnmarshalJSON(res, &result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
|
||||
anyPub, err := codectypes.NewAnyWithValue(vals[0].PubKey)
|
||||
@@ -245,7 +245,7 @@ func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPCGateway() {
|
||||
s.Require().Contains(string(res), tc.expErrMsg)
|
||||
} else {
|
||||
var result tmservice.GetValidatorSetByHeightResponse
|
||||
err = vals[0].ClientCtx.JSONCodec.UnmarshalJSON(res, &result)
|
||||
err = vals[0].ClientCtx.Codec.UnmarshalJSON(res, &result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
|
||||
}
|
||||
|
||||
+1
-1
@@ -172,7 +172,7 @@ func RunAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
|
||||
pubKey, _ := cmd.Flags().GetString(FlagPublicKey)
|
||||
if pubKey != "" {
|
||||
var pk cryptotypes.PubKey
|
||||
err = ctx.JSONCodec.UnmarshalInterfaceJSON([]byte(pubKey), &pk)
|
||||
err = ctx.Codec.UnmarshalInterfaceJSON([]byte(pubKey), &pk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user