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

refactor(client): add client/Context.Codec and deprecate JSONCodec #9498

Merged
merged 8 commits into from
Jun 11, 2021
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ if input key is empty, or input data contains empty key.
* (x/staking) [\#9214](https://github.com/cosmos/cosmos-sdk/pull/9214) Added `new_shares` attribute inside `EventTypeDelegate` event.
* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function.
* [\#9457](https://github.com/cosmos/cosmos-sdk/pull/9457) Add amino support for x/authz and x/feegrant Msgs.
* [\#9498](https://github.com/cosmos/cosmos-sdk/pull/9498) Added `Codec: codec.Codec` attribute to `client/Context` structure.

### Client Breaking Changes

Expand Down Expand Up @@ -155,6 +156,8 @@ if input key is empty, or input data contains empty key.
### Deprecated

* (grpc) [\#8926](https://github.com/cosmos/cosmos-sdk/pull/8926) The `tx` field in `SimulateRequest` has been deprecated, prefer to pass `tx_bytes` instead.
* (sdk types) [\#9498](https://github.com/cosmos/cosmos-sdk/pull/9498) `clientContext.JSONCodec` will be removed in the next version. use `clientContext.Codec` instead.


## [v0.42.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.42.4) - 2021-04-08

Expand Down
26 changes: 20 additions & 6 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 7 additions & 7 deletions client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions docs/architecture/adr-020-protobuf-transaction-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
- 2020 September 25: Remove `PublicKey` type in favor of `secp256k1.PubKey`, `ed25519.PubKey` and `multisig.LegacyAminoPubKey`.
- 2020 October 15: Add `GetAccount` and `GetAccountWithHeight` methods to the `AccountRetriever` interface.
- 2021 Feb 24: The SDK does not use Tendermint's `PubKey` interface anymore, but its own `cryptotypes.PubKey`. Updates to reflect this.
- 2021 May 3: Rename `clientCtx.JSONMarshaler` to `clientCtx.JSONCodec`
- 2021 May 3: Rename `clientCtx.JSONMarshaler` to `clientCtx.JSONCodec`.
- 2021 June 10: Add `clientCtx.Codec: codec.Codec`.

## Status

Expand Down Expand Up @@ -344,7 +345,7 @@ type TxBuilder interface {
}
```

We then update `Context` to have new fields: `JSONCodec`, `TxGenerator`,
We then update `Context` to have new fields: `Codec`, `TxGenerator`,
and `AccountRetriever`, and we update `AppModuleBasic.GetTxCmd` to take
a `Context` which should have all of these fields pre-populated.

Expand Down
6 changes: 3 additions & 3 deletions docs/migrations/app_and_modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ clientCtx, err := client.GetClientTxContext(cmd)

Some other flags helper functions are transformed: `flags.PostCommands(cmds ...*cobra.Command) []*cobra.Command` and `flags.GetCommands(...)` usage is now replaced by `flags.AddTxFlagsToCmd(cmd *cobra.Command)` and `flags.AddQueryFlagsToCmd(cmd *cobra.Command)` respectively.

Moreover, new CLI commands don't take any codec as input anymore. Instead, the `clientCtx` can be retrieved from the `cmd` itself using the `GetClient{Query,Tx}Context` function above, and the codec as `clientCtx.JSONCodec`.
Moreover, new CLI commands don't take any codec as input anymore. Instead, the `clientCtx` can be retrieved from the `cmd` itself using the `GetClient{Query,Tx}Context` function above, and the codec as `clientCtx.Codec`.

```diff
// v0.39
- func SendTxCmd(cdc *codec.Codec) *cobra.Command {
- cdc.MarshalJSON(...)
- }

// v0.40
// v0.43
+ func NewSendTxCmd() *cobra.Command {
+ clientCtx, err := client.GetClientTxContext(cmd)
+ clientCtx.JSONCodec.MarshalJSON(...)
+ clientCtx.Codec.MarshalJSON(...)
+}
```

Expand Down
2 changes: 1 addition & 1 deletion server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *t
serverCtx := server.NewDefaultContext()
serverCtx.Config.RootDir = tempDir

clientCtx := client.Context{}.WithJSONCodec(app.AppCodec())
clientCtx := client.Context{}.WithCodec(app.AppCodec())
genDoc := newDefaultGenesisDoc(encCfg.Marshaler)

require.NoError(t, saveGenesisFile(genDoc, serverCtx.Config.GenesisFile()))
Expand Down
2 changes: 1 addition & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
Retries: config.Rosetta.Retries,
Offline: offlineMode,
}
conf.WithCodec(clientCtx.InterfaceRegistry, clientCtx.JSONCodec.(*codec.ProtoCodec))
conf.WithCodec(clientCtx.InterfaceRegistry, clientCtx.Codec.(*codec.ProtoCodec))

rosettaSrv, err = rosetta.ServerFromConfig(conf)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ShowValidatorCmd() *cobra.Command {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)
bz, err := clientCtx.JSONCodec.MarshalInterfaceJSON(sdkPK)
bz, err := clientCtx.Codec.MarshalInterfaceJSON(sdkPK)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions simapp/params/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
// This is provided for compatibility between protobuf and amino implementations.
type EncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Marshaler codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
// NOTE: this field will be renamed to Codec
Marshaler codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
2 changes: 1 addition & 1 deletion simapp/simd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
depCdc := clientCtx.JSONCodec
depCdc := clientCtx.Codec
cdc := depCdc.(codec.Codec)

serverCtx := server.GetServerContextFromCmd(cmd)
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/genaccounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestAddGenesisAccountCmd(t *testing.T) {
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithJSONCodec(appCodec).WithHomeDir(home)
clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
encodingConfig := simapp.MakeTestEncodingConfig()
initClientCtx := client.Context{}.
WithJSONCodec(encodingConfig.Marshaler).
WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
Expand Down
12 changes: 6 additions & 6 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,26 +271,26 @@ func initGenFiles(
genFiles []string, numValidators int,
) error {

appGenState := mbm.DefaultGenesis(clientCtx.JSONCodec)
appGenState := mbm.DefaultGenesis(clientCtx.Codec)

// set the accounts in the genesis state
var authGenState authtypes.GenesisState
clientCtx.JSONCodec.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState)
clientCtx.Codec.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState)

accounts, err := authtypes.PackAccounts(genAccounts)
if err != nil {
return err
}

authGenState.Accounts = accounts
appGenState[authtypes.ModuleName] = clientCtx.JSONCodec.MustMarshalJSON(&authGenState)
appGenState[authtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&authGenState)

// set the balances in the genesis state
var bankGenState banktypes.GenesisState
clientCtx.JSONCodec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)
clientCtx.Codec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)

bankGenState.Balances = genBalances
appGenState[banktypes.ModuleName] = clientCtx.JSONCodec.MustMarshalJSON(&bankGenState)
appGenState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&bankGenState)

appGenStateJSON, err := json.MarshalIndent(appGenState, "", " ")
if err != nil {
Expand Down Expand Up @@ -337,7 +337,7 @@ func collectGenFiles(
return err
}

nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.JSONCodec, clientCtx.TxConfig, nodeConfig, initCfg, *genDoc, genBalIterator)
nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.Codec, clientCtx.TxConfig, nodeConfig, initCfg, *genDoc, genBalIterator)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func New(t *testing.T, cfg Config) *Network {
WithHomeDir(tmCfg.RootDir).
WithChainID(cfg.ChainID).
WithInterfaceRegistry(cfg.InterfaceRegistry).
WithJSONCodec(cfg.Codec).
WithCodec(cfg.Codec).
WithLegacyAmino(cfg.LegacyAmino).
WithTxConfig(cfg.TxConfig).
WithAccountRetriever(cfg.AccountRetriever)
Expand Down
4 changes: 2 additions & 2 deletions x/auth/client/cli/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestGetCommandEncode(t *testing.T) {
ctx := context.Background()
clientCtx := client.Context{}.
WithTxConfig(encodingConfig.TxConfig).
WithJSONCodec(encodingConfig.Marshaler)
WithCodec(encodingConfig.Marshaler)
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

cmd.SetArgs([]string{txFileName})
Expand All @@ -52,7 +52,7 @@ func TestGetCommandDecode(t *testing.T) {

clientCtx := client.Context{}.
WithTxConfig(encodingConfig.TxConfig).
WithJSONCodec(encodingConfig.Marshaler)
WithCodec(encodingConfig.Marshaler)

cmd := GetDecodeCommand()
_ = testutil.ApplyMockIODiscardOutErr(cmd)
Expand Down
6 changes: 3 additions & 3 deletions x/auth/client/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (s *IntegrationTestSuite) TestQueryTx() {

s.Require().NoError(err)
var txRes sdk.TxResponse
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &txRes))
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes))
s.Require().Equal(uint32(0), txRes.Code)

s.Require().NoError(s.network.WaitForNextBlock())
Expand Down Expand Up @@ -411,7 +411,7 @@ func (s *IntegrationTestSuite) testQueryIBCTx(txRes sdk.TxResponse, cmd *cobra.C
s.Require().NoError(err)

var getTxRes txtypes.GetTxResponse
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(grpcJSON, &getTxRes))
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(grpcJSON, &getTxRes))
s.Require().Equal(getTxRes.Tx.Body.Memo, "foobar")

// generate broadcast only txn.
Expand Down Expand Up @@ -522,7 +522,7 @@ func (s *IntegrationTestSuite) TestLegacyMultisig() {
s.Require().NoError(s.network.WaitForNextBlock())

var txRes sdk.TxResponse
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &txRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes)
s.Require().NoError(err)
s.Require().Equal(uint32(0), txRes.Code)

Expand Down
Loading