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

Ensure all the empty arrays return an empty slice #5248

Merged
merged 8 commits into from
Oct 28, 2019
7 changes: 6 additions & 1 deletion x/bank/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}

bz, err := codec.MarshalJSONIndent(types.ModuleCdc, k.GetCoins(ctx, params.Address))
coins := k.GetCoins(ctx, params.Address)
if coins == nil {
coins = sdk.NewCoins()
}

bz, err := codec.MarshalJSONIndent(types.ModuleCdc, coins)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
Expand Down
18 changes: 16 additions & 2 deletions x/distribution/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ func queryValidatorOutstandingRewards(ctx sdk.Context, path []string, req abci.R
if err != nil {
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
}
bz, err := codec.MarshalJSONIndent(k.cdc, k.GetValidatorOutstandingRewards(ctx, params.ValidatorAddress))
rewards := k.GetValidatorOutstandingRewards(ctx, params.ValidatorAddress)
if rewards == nil {
rewards = sdk.DecCoins{}
}
bz, err := codec.MarshalJSONIndent(k.cdc, rewards)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
Expand All @@ -99,6 +103,9 @@ func queryValidatorCommission(ctx sdk.Context, path []string, req abci.RequestQu
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
}
commission := k.GetValidatorAccumulatedCommission(ctx, params.ValidatorAddress)
if commission == nil {
commission = sdk.DecCoins{}
}
bz, err := codec.MarshalJSONIndent(k.cdc, commission)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
Expand Down Expand Up @@ -150,6 +157,9 @@ func queryDelegationRewards(ctx sdk.Context, _ []string, req abci.RequestQuery,

endingPeriod := k.incrementValidatorPeriod(ctx, val)
rewards := k.calculateDelegationRewards(ctx, val, del, endingPeriod)
if rewards == nil {
rewards = sdk.DecCoins{}
}

bz, err := codec.MarshalJSONIndent(k.cdc, rewards)
if err != nil {
Expand Down Expand Up @@ -242,7 +252,11 @@ func queryDelegatorWithdrawAddress(ctx sdk.Context, _ []string, req abci.Request
}

func queryCommunityPool(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
bz, err := k.cdc.MarshalJSON(k.GetFeePoolCommunityCoins(ctx))
pool := k.GetFeePoolCommunityCoins(ctx)
if pool == nil {
pool = sdk.DecCoins{}
}
bz, err := k.cdc.MarshalJSON(pool)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
Expand Down
13 changes: 12 additions & 1 deletion x/gov/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
}

deposits := keeper.GetDeposits(ctx, params.ProposalID)
if deposits == nil {
deposits = types.Deposits{}
}

bz, err := codec.MarshalJSONIndent(keeper.cdc, deposits)
if err != nil {
Expand Down Expand Up @@ -182,6 +185,9 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
}

votes := keeper.GetVotes(ctx, params.ProposalID)
if votes == nil {
votes = types.Votes{}
}

bz, err := codec.MarshalJSONIndent(keeper.cdc, votes)
if err != nil {
Expand All @@ -198,7 +204,12 @@ func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper K
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("failed to parse params", err.Error()))
}

bz, err := codec.MarshalJSONIndent(keeper.cdc, keeper.GetProposalsFiltered(ctx, params))
proposals := keeper.GetProposalsFiltered(ctx, params)
if proposals == nil {
proposals = types.Proposals{}
}

bz, err := codec.MarshalJSONIndent(keeper.cdc, proposals)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
Expand Down
21 changes: 21 additions & 0 deletions x/staking/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func queryValidatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper)
return nil, sdk.ErrInternal(err.Error())
}

if delegationResps == nil {
delegationResps = types.DelegationResponses{}
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, delegationResps)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", err.Error()))
Expand All @@ -132,6 +136,9 @@ func queryValidatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery,
}

unbonds := k.GetUnbondingDelegationsFromValidator(ctx, params.ValidatorAddr)
if unbonds == nil {
unbonds = types.UnbondingDelegations{}
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbonds)
if err != nil {
Expand All @@ -155,6 +162,10 @@ func queryDelegatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper)
return nil, sdk.ErrInternal(err.Error())
}

if delegationResps == nil {
delegationResps = types.DelegationResponses{}
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, delegationResps)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", err.Error()))
Expand All @@ -172,6 +183,9 @@ func queryDelegatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery,
}

unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
if unbondingDelegations == nil {
unbondingDelegations = types.UnbondingDelegations{}
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbondingDelegations)
if err != nil {
Expand All @@ -192,6 +206,9 @@ func queryDelegatorValidators(ctx sdk.Context, req abci.RequestQuery, k Keeper)
}

validators := k.GetDelegatorValidators(ctx, params.DelegatorAddr, stakingParams.MaxValidators)
if validators == nil {
validators = types.Validators{}
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, validators)
if err != nil {
Expand Down Expand Up @@ -298,6 +315,10 @@ func queryRedelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byt
return nil, sdk.ErrInternal(err.Error())
}

if redelResponses == nil {
redelResponses = types.RedelegationResponses{}
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, redelResponses)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", err.Error()))
Expand Down