Skip to content

Commit

Permalink
JSON Codec Updates (cosmos#6444)
Browse files Browse the repository at this point in the history
* Initial commit

* More updates

* Fix tests

* CLI test updates

* Updates

* Updates
  • Loading branch information
alexanderbez committed Jun 16, 2020
1 parent 6a05b83 commit 35312d0
Show file tree
Hide file tree
Showing 35 changed files with 159 additions and 126 deletions.
6 changes: 3 additions & 3 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ func (ctx Context) PrintOutput(toPrint interface{}) error {
out, err = yaml.Marshal(&toPrint)

case "json":
out, err = ctx.JSONMarshaler.MarshalJSON(toPrint)

if ctx.Indent {
out, err = ctx.Codec.MarshalJSONIndent(toPrint, "", " ")
} else {
out, err = ctx.Codec.MarshalJSON(toPrint)
out, err = codec.MarshalIndentFromJSON(out)
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/cli/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $ <appcli> tx broadcast ./mytxn.json
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

if clientCtx.Offline {
return errors.New("cannot broadcast tx during offline mode")
Expand Down
4 changes: 2 additions & 2 deletions x/auth/client/cli/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func GetDecodeCommand(codec *codec.Codec) *cobra.Command {
return flags.PostCommands(cmd)[0]
}

func runDecodeTxString(codec *codec.Codec) func(cmd *cobra.Command, args []string) (err error) {
func runDecodeTxString(cdc *codec.Codec) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.NewContext().WithCodec(codec).WithOutput(cmd.OutOrStdout())
clientCtx := client.NewContext().WithCodec(cdc).WithOutput(cmd.OutOrStdout()).WithJSONMarshaler(cdc)
var txBytes []byte

if viper.GetBool(flagHex) {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/cli/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Read a transaction from <file>, serialize it to the Amino wire protocol, and out
If you supply a dash (-) argument in place of an input filename, the command reads from standard input.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

stdTx, err := authclient.ReadStdTxFromFile(clientCtx.Codec, args[0])
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, clientCtx client.Context,
return
}

output, err := clientCtx.Codec.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
output, err := clientCtx.JSONMarshaler.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
if rest.CheckInternalServerError(w, err) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/rest/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func BroadcastTxRequest(clientCtx client.Context) http.HandlerFunc {
return
}

if err := clientCtx.Codec.UnmarshalJSON(body, &req); rest.CheckBadRequestError(w, err) {
if err := clientCtx.JSONMarshaler.UnmarshalJSON(body, &req); rest.CheckBadRequestError(w, err) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/rest/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func DecodeTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

err = clientCtx.Codec.UnmarshalJSON(body, &req)
err = clientCtx.JSONMarshaler.UnmarshalJSON(body, &req)
if rest.CheckBadRequestError(w, err) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/rest/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func EncodeTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

err = clientCtx.Codec.UnmarshalJSON(body, &req)
err = clientCtx.JSONMarshaler.UnmarshalJSON(body, &req)
if rest.CheckBadRequestError(w, err) {
return
}
Expand Down
20 changes: 10 additions & 10 deletions x/auth/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ func CompleteAndBroadcastTxCLI(txBldr authtypes.TxBuilder, clientCtx client.Cont
return err
}

var json []byte
json := clientCtx.JSONMarshaler.MustMarshalJSON(stdSignMsg)
if viper.GetBool(flags.FlagIndentResponse) {
json, err = clientCtx.Codec.MarshalJSONIndent(stdSignMsg, "", " ")
json, err = codec.MarshalIndentFromJSON(json)
if err != nil {
panic(err)
}
} else {
json = clientCtx.Codec.MustMarshalJSON(stdSignMsg)
}

_, _ = fmt.Fprintf(os.Stderr, "%s\n\n", json)
Expand Down Expand Up @@ -161,16 +159,18 @@ func PrintUnsignedStdTx(txBldr authtypes.TxBuilder, clientCtx client.Context, ms
return err
}

var json []byte
if viper.GetBool(flags.FlagIndentResponse) {
json, err = clientCtx.Codec.MarshalJSONIndent(stdTx, "", " ")
} else {
json, err = clientCtx.Codec.MarshalJSON(stdTx)
}
json, err := clientCtx.JSONMarshaler.MarshalJSON(stdTx)
if err != nil {
return err
}

if viper.GetBool(flags.FlagIndentResponse) {
json, err = codec.MarshalIndentFromJSON(json)
if err != nil {
return err
}
}

_, _ = fmt.Fprintf(clientCtx.Output, "%s\n", json)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetBalancesCmd(cdc *codec.Codec) *cobra.Command {
Short: "Query for account balances by address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
Expand Down Expand Up @@ -135,7 +135,7 @@ $ %s query %s total stake
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

if len(args) == 0 {
return queryTotalSupply(clientCtx, cdc)
Expand Down
4 changes: 2 additions & 2 deletions x/bank/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func totalSupplyHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

params := types.NewQueryTotalSupplyParams(page, limit)
bz, err := clientCtx.Codec.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)

if rest.CheckBadRequestError(w, err) {
return
Expand All @@ -112,7 +112,7 @@ func supplyOfHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

params := types.NewQuerySupplyOfParams(denom)
bz, err := clientCtx.Codec.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)

if rest.CheckBadRequestError(w, err) {
return
Expand Down
12 changes: 6 additions & 6 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
Args: cobra.NoArgs,
Short: "Query distribution params",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := clientCtx.QueryWithData(route, nil)
Expand Down Expand Up @@ -80,7 +80,7 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ $ %s query distribution commission cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9l
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
Expand Down Expand Up @@ -162,7 +162,7 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
Expand Down Expand Up @@ -216,7 +216,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

// query for rewards from a particular delegation
if len(args) == 2 {
Expand Down Expand Up @@ -277,7 +277,7 @@ $ %s query distribution community-pool
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions x/distribution/client/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func QueryDelegationRewards(clientCtx client.Context, queryRoute, delAddr, valAd
}

params := types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)
bz, err := clientCtx.Codec.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
return nil, 0, fmt.Errorf("failed to marshal params: %w", err)
}
Expand All @@ -36,7 +36,7 @@ func QueryDelegationRewards(clientCtx client.Context, queryRoute, delAddr, valAd
func QueryDelegatorValidators(clientCtx client.Context, queryRoute string, delegatorAddr sdk.AccAddress) ([]byte, error) {
res, _, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorValidators),
clientCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
clientCtx.JSONMarshaler.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
)
return res, err
}
Expand All @@ -45,7 +45,7 @@ func QueryDelegatorValidators(clientCtx client.Context, queryRoute string, deleg
func QueryValidatorCommission(clientCtx client.Context, queryRoute string, validatorAddr sdk.ValAddress) ([]byte, error) {
res, _, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorCommission),
clientCtx.Codec.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
clientCtx.JSONMarshaler.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
)
return res, err
}
Expand All @@ -61,7 +61,7 @@ func WithdrawAllDelegatorRewards(clientCtx client.Context, queryRoute string, de
}

var validators []sdk.ValAddress
if err := clientCtx.Codec.UnmarshalJSON(bz, &validators); err != nil {
if err := clientCtx.JSONMarshaler.UnmarshalJSON(bz, &validators); err != nil {
return nil, err
}

Expand Down
8 changes: 5 additions & 3 deletions x/distribution/client/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

func TestQueryDelegationRewardsAddrValidation(t *testing.T) {
cdc := codec.New()
viper.Set(flags.FlagOffline, true)
ctx := client.NewContext().WithCodec(cdc)
ctx := client.NewContext().WithJSONMarshaler(types.ModuleCdc)

type args struct {
delAddr string
valAddr string
}

tests := []struct {
name string
args args
Expand All @@ -30,6 +31,7 @@ func TestQueryDelegationRewardsAddrValidation(t *testing.T) {
{"invalid validator address", args{"cosmos1zxcsu7l5qxs53lvp0fqgd09a9r2g6kqrk2cdpa", "invalid"}, nil, true},
{"empty validator address", args{"cosmos1zxcsu7l5qxs53lvp0fqgd09a9r2g6kqrk2cdpa", ""}, nil, true},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions x/distribution/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func delegatorRewardsHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

params := types.NewQueryDelegatorParams(delegatorAddr)
bz, err := clientCtx.Codec.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal params: %s", err))
return
Expand Down Expand Up @@ -131,7 +131,7 @@ func delegatorWithdrawalAddrHandlerFn(clientCtx client.Context) http.HandlerFunc
return
}

bz := clientCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr))
bz := clientCtx.JSONMarshaler.MustMarshalJSON(types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr))
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraw_addr", types.QuerierRoute), bz)
if rest.CheckInternalServerError(w, err) {
return
Expand Down Expand Up @@ -180,7 +180,7 @@ func validatorInfoHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

var commission types.ValidatorAccumulatedCommission
if rest.CheckInternalServerError(w, clientCtx.Codec.UnmarshalJSON(bz, &commission)) {
if rest.CheckInternalServerError(w, clientCtx.JSONMarshaler.UnmarshalJSON(bz, &commission)) {
return
}

Expand All @@ -192,11 +192,11 @@ func validatorInfoHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

var rewards sdk.DecCoins
if rest.CheckInternalServerError(w, clientCtx.Codec.UnmarshalJSON(bz, &rewards)) {
if rest.CheckInternalServerError(w, clientCtx.JSONMarshaler.UnmarshalJSON(bz, &rewards)) {
return
}

bz, err = clientCtx.Codec.MarshalJSON(NewValidatorDistInfo(delAddr, rewards, commission))
bz, err = clientCtx.JSONMarshaler.MarshalJSON(NewValidatorDistInfo(delAddr, rewards, commission))
if rest.CheckInternalServerError(w, err) {
return
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func communityPoolHandler(clientCtx client.Context) http.HandlerFunc {
}

var result sdk.DecCoins
if rest.CheckInternalServerError(w, clientCtx.Codec.UnmarshalJSON(res, &result)) {
if rest.CheckInternalServerError(w, clientCtx.JSONMarshaler.UnmarshalJSON(res, &result)) {
return
}

Expand All @@ -285,7 +285,7 @@ func outstandingRewardsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

bin := clientCtx.Codec.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr))
bin := clientCtx.JSONMarshaler.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr))
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", types.QuerierRoute), bin)
if rest.CheckInternalServerError(w, err) {
return
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func QueryEvidenceCmd(cdc *codec.Codec) func(*cobra.Command, []string) error {
return err
}

clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc)

if hash := args[0]; hash != "" {
return queryEvidence(cdc, clientCtx, hash)
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func queryEvidenceHandler(clientCtx client.Context) http.HandlerFunc {
}

params := types.NewQueryEvidenceParams(evidenceHash)
bz, err := clientCtx.Codec.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
return
Expand Down Expand Up @@ -70,7 +70,7 @@ func queryAllEvidenceHandler(clientCtx client.Context) http.HandlerFunc {
}

params := types.NewQueryAllEvidenceParams(page, limit)
bz, err := clientCtx.Codec.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
return
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, sm
}

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
clientCtx := client.NewContextWithInput(inBuf).WithCodec(cdc)
clientCtx := client.NewContextWithInput(inBuf).WithCodec(cdc).WithJSONMarshaler(cdc)

// Set the generate-only flag here after the CLI context has
// been created. This allows the from name/key to be correctly populated.
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func QueryGenesisTxs(clientCtx client.Context, w http.ResponseWriter) {
genState := types.GetGenesisStateFromAppState(clientCtx.Codec, appState)
genTxs := make([]sdk.Tx, len(genState.GenTxs))
for i, tx := range genState.GenTxs {
err := clientCtx.Codec.UnmarshalJSON(tx, &genTxs[i])
err := clientCtx.JSONMarshaler.UnmarshalJSON(tx, &genTxs[i])
if err != nil {
rest.WriteErrorResponse(
w, http.StatusInternalServerError,
Expand Down
Loading

0 comments on commit 35312d0

Please sign in to comment.