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:
@@ -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})
|
||||
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
@@ -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.
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ func (s *IntegrationTestSuite) TestCLISignAminoJSON() {
|
||||
queryResJSON, err := QueryAccountExec(val1.ClientCtx, val1.Address)
|
||||
require.NoError(err)
|
||||
var account authtypes.AccountI
|
||||
require.NoError(val1.ClientCtx.JSONCodec.UnmarshalInterfaceJSON(queryResJSON.Bytes(), &account))
|
||||
require.NoError(val1.ClientCtx.Codec.UnmarshalInterfaceJSON(queryResJSON.Bytes(), &account))
|
||||
|
||||
/**** test signature-only ****/
|
||||
res, err := TxSignExec(val1.ClientCtx, val1.Address, fileUnsigned.Name(), chainFlag,
|
||||
@@ -258,7 +258,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxCmd() {
|
||||
)
|
||||
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().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
testCases := []struct {
|
||||
@@ -300,7 +300,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxCmd() {
|
||||
s.Require().NotEqual("internal", err.Error())
|
||||
} else {
|
||||
var result sdk.TxResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &result))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &result))
|
||||
s.Require().NotNil(result.Height)
|
||||
s.Require().Contains(result.RawLog, tc.rawLogContains)
|
||||
}
|
||||
@@ -353,7 +353,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
|
||||
s.Require().NoError(err)
|
||||
|
||||
var balRes banktypes.QueryAllBalancesResponse
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
startTokens := balRes.Balances.AmountOf(s.cfg.BondDenom)
|
||||
|
||||
@@ -414,7 +414,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
|
||||
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(startTokens, balRes.Balances.AmountOf(s.cfg.BondDenom))
|
||||
|
||||
@@ -437,7 +437,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
|
||||
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, account.GetAddress())
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(sendTokens.Amount, balRes.Balances.AmountOf(s.cfg.BondDenom))
|
||||
|
||||
@@ -445,7 +445,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
|
||||
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
@@ -554,7 +554,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
|
||||
s.Require().NoError(err)
|
||||
|
||||
var balRes banktypes.QueryAllBalancesResponse
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
intialCoins := balRes.Balances
|
||||
|
||||
@@ -572,7 +572,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
|
||||
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, multisigInfo.GetAddress())
|
||||
s.Require().NoError(err)
|
||||
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
diff, _ := balRes.Balances.SafeSub(intialCoins)
|
||||
s.Require().Equal(sendTokens.Amount, diff.AmountOf(s.cfg.BondDenom))
|
||||
@@ -650,7 +650,7 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
|
||||
s.Require().NoError(err)
|
||||
|
||||
var balRes banktypes.QueryAllBalancesResponse
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(sendTokens.Amount, balRes.Balances.AmountOf(s.cfg.BondDenom))
|
||||
|
||||
@@ -805,7 +805,7 @@ func (s *IntegrationTestSuite) TestMultisignBatch() {
|
||||
queryResJSON, err := QueryAccountExec(val.ClientCtx, multisigInfo.GetAddress())
|
||||
s.Require().NoError(err)
|
||||
var account authtypes.AccountI
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalInterfaceJSON(queryResJSON.Bytes(), &account))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalInterfaceJSON(queryResJSON.Bytes(), &account))
|
||||
|
||||
// sign-batch file
|
||||
res, err := TxSignBatchExec(val.ClientCtx, account1.GetAddress(), filename.Name(), fmt.Sprintf("--%s=%s", flags.FlagChainID, val.ClientCtx.ChainID), "--multisig", multisigInfo.GetAddress().String(), fmt.Sprintf("--%s", flags.FlagOffline), fmt.Sprintf("--%s=%s", flags.FlagAccountNumber, fmt.Sprint(account.GetAccountNumber())), fmt.Sprintf("--%s=%s", flags.FlagSequence, fmt.Sprint(account.GetSequence())))
|
||||
@@ -867,7 +867,7 @@ func (s *IntegrationTestSuite) TestGetAccountCmd() {
|
||||
s.Require().NotEqual("internal", err.Error())
|
||||
} else {
|
||||
var acc authtypes.AccountI
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalInterfaceJSON(out.Bytes(), &acc))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalInterfaceJSON(out.Bytes(), &acc))
|
||||
s.Require().Equal(val.Address, acc.GetAddress())
|
||||
}
|
||||
})
|
||||
@@ -884,7 +884,7 @@ func (s *IntegrationTestSuite) TestGetAccountsCmd() {
|
||||
s.Require().NoError(err)
|
||||
|
||||
var res authtypes.QueryAccountsResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &res))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
|
||||
s.Require().NotEmpty(res.Accounts)
|
||||
}
|
||||
|
||||
@@ -962,7 +962,7 @@ func (s *IntegrationTestSuite) TestQueryParamsCmd() {
|
||||
s.Require().NotEqual("internal", err.Error())
|
||||
} else {
|
||||
var authParams authtypes.Params
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &authParams))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &authParams))
|
||||
s.Require().NotNil(authParams.MaxMemoCharacters)
|
||||
}
|
||||
})
|
||||
@@ -1009,11 +1009,11 @@ func (s *IntegrationTestSuite) TestTxWithoutPublicKey() {
|
||||
// Note: this method is only used for test purposes! In general, one should
|
||||
// use txBuilder and TxEncoder/TxDecoder to manipulate txs.
|
||||
var tx tx.Tx
|
||||
err = val1.ClientCtx.JSONCodec.UnmarshalJSON(signedTx.Bytes(), &tx)
|
||||
err = val1.ClientCtx.Codec.UnmarshalJSON(signedTx.Bytes(), &tx)
|
||||
s.Require().NoError(err)
|
||||
tx.AuthInfo.SignerInfos[0].PublicKey = nil
|
||||
// Re-encode the tx again, to another file.
|
||||
txJSON, err = val1.ClientCtx.JSONCodec.MarshalJSON(&tx)
|
||||
txJSON, err = val1.ClientCtx.Codec.MarshalJSON(&tx)
|
||||
s.Require().NoError(err)
|
||||
signedTxFile := testutil.WriteToNewTempFile(s.T(), string(txJSON))
|
||||
s.Require().True(strings.Contains(string(txJSON), "\"public_key\":null"))
|
||||
@@ -1023,7 +1023,7 @@ func (s *IntegrationTestSuite) TestTxWithoutPublicKey() {
|
||||
out, err := TxBroadcastExec(val1.ClientCtx, signedTxFile.Name())
|
||||
s.Require().NoError(err)
|
||||
var res sdk.TxResponse
|
||||
s.Require().NoError(val1.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &res))
|
||||
s.Require().NoError(val1.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
|
||||
s.Require().NotEqual(0, res.Code)
|
||||
}
|
||||
|
||||
@@ -1081,14 +1081,14 @@ func (s *IntegrationTestSuite) TestSignWithMultiSignersAminoJSON() {
|
||||
|
||||
require.NoError(err)
|
||||
var txRes sdk.TxResponse
|
||||
require.NoError(val0.ClientCtx.JSONCodec.UnmarshalJSON(res.Bytes(), &txRes))
|
||||
require.NoError(val0.ClientCtx.Codec.UnmarshalJSON(res.Bytes(), &txRes))
|
||||
require.Equal(uint32(0), txRes.Code)
|
||||
|
||||
// Make sure the addr1's balance got funded.
|
||||
queryResJSON, err := bankcli.QueryBalancesExec(val0.ClientCtx, addr1)
|
||||
require.NoError(err)
|
||||
var queryRes banktypes.QueryAllBalancesResponse
|
||||
err = val0.ClientCtx.JSONCodec.UnmarshalJSON(queryResJSON.Bytes(), &queryRes)
|
||||
err = val0.ClientCtx.Codec.UnmarshalJSON(queryResJSON.Bytes(), &queryRes)
|
||||
require.NoError(err)
|
||||
require.Equal(sdk.NewCoins(val0Coin, val1Coin), queryRes.Balances)
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ func TestMigrate(t *testing.T) {
|
||||
}
|
||||
}`
|
||||
|
||||
bz, err := clientCtx.JSONCodec.MarshalJSON(migrated)
|
||||
bz, err := clientCtx.Codec.MarshalJSON(migrated)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Indent the JSON bz correctly.
|
||||
|
||||
@@ -76,7 +76,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
|
||||
fmt.Sprintf("--%s=foobar", flags.FlagNote),
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(val.ClientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &s.txRes))
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &s.txRes))
|
||||
s.Require().Equal(uint32(0), s.txRes.Code)
|
||||
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
@@ -151,7 +151,7 @@ func (s IntegrationTestSuite) TestSimulateTx_GRPCGateway() {
|
||||
|
||||
for _, tc := range testCases {
|
||||
s.Run(tc.name, func() {
|
||||
req, err := val.ClientCtx.JSONCodec.MarshalJSON(tc.req)
|
||||
req, err := val.ClientCtx.Codec.MarshalJSON(tc.req)
|
||||
s.Require().NoError(err)
|
||||
res, err := rest.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/simulate", val.APIAddress), "application/json", req)
|
||||
s.Require().NoError(err)
|
||||
@@ -159,7 +159,7 @@ func (s IntegrationTestSuite) TestSimulateTx_GRPCGateway() {
|
||||
s.Require().Contains(string(res), tc.expErrMsg)
|
||||
} else {
|
||||
var result tx.SimulateResponse
|
||||
err = val.ClientCtx.JSONCodec.UnmarshalJSON(res, &result)
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
|
||||
s.Require().NoError(err)
|
||||
// Check the result and gas used are correct.
|
||||
s.Require().Equal(len(result.GetResult().GetEvents()), 6) // 1 coin recv, 1 coin spent,1 transfer, 3 messages.
|
||||
@@ -313,7 +313,7 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPCGateway() {
|
||||
s.Require().Contains(string(res), tc.expErrMsg)
|
||||
} else {
|
||||
var result tx.GetTxsEventResponse
|
||||
err = val.ClientCtx.JSONCodec.UnmarshalJSON(res, &result)
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().GreaterOrEqual(len(result.Txs), 1)
|
||||
s.Require().Equal("foobar", result.Txs[0].Body.Memo)
|
||||
@@ -382,7 +382,7 @@ func (s IntegrationTestSuite) TestGetTx_GRPCGateway() {
|
||||
s.Require().Contains(string(res), tc.expErrMsg)
|
||||
} else {
|
||||
var result tx.GetTxResponse
|
||||
err = val.ClientCtx.JSONCodec.UnmarshalJSON(res, &result)
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal("foobar", result.Tx.Body.Memo)
|
||||
s.Require().NotZero(result.TxResponse.Height)
|
||||
@@ -459,7 +459,7 @@ func (s IntegrationTestSuite) TestBroadcastTx_GRPCGateway() {
|
||||
|
||||
for _, tc := range testCases {
|
||||
s.Run(tc.name, func() {
|
||||
req, err := val.ClientCtx.JSONCodec.MarshalJSON(tc.req)
|
||||
req, err := val.ClientCtx.Codec.MarshalJSON(tc.req)
|
||||
s.Require().NoError(err)
|
||||
res, err := rest.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", val.APIAddress), "application/json", req)
|
||||
s.Require().NoError(err)
|
||||
@@ -467,7 +467,7 @@ func (s IntegrationTestSuite) TestBroadcastTx_GRPCGateway() {
|
||||
s.Require().Contains(string(res), tc.expErrMsg)
|
||||
} else {
|
||||
var result tx.BroadcastTxResponse
|
||||
err = val.ClientCtx.JSONCodec.UnmarshalJSON(res, &result)
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(uint32(0), result.TxResponse.Code, "rawlog", result.TxResponse.RawLog)
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ func (s *IntegrationTestSuite) TestNewMsgCreateVestingAccountCmd() {
|
||||
s.Require().Error(err)
|
||||
} else {
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(clientCtx.JSONCodec.UnmarshalJSON(bw.Bytes(), tc.respType), bw.String())
|
||||
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bw.Bytes(), tc.respType), bw.String())
|
||||
|
||||
txResp := tc.respType.(*sdk.TxResponse)
|
||||
s.Require().Equal(tc.expectedCode, txResp.Code)
|
||||
|
||||
Reference in New Issue
Block a user