From 29ace1706e2ed3808216a746e5c3115b19358366 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 25 Oct 2019 21:05:03 +0200 Subject: [PATCH 1/7] Ensure all the empty arrays return an empty slice --- x/bank/internal/keeper/querier.go | 7 ++++++- x/distribution/keeper/querier.go | 18 ++++++++++++++++-- x/gov/keeper/querier.go | 13 ++++++++++++- x/staking/keeper/querier.go | 21 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/x/bank/internal/keeper/querier.go b/x/bank/internal/keeper/querier.go index 5a0fd3910613..f7622d40deb9 100644 --- a/x/bank/internal/keeper/querier.go +++ b/x/bank/internal/keeper/querier.go @@ -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 = make(sdk.Coins, 0) + } + + bz, err := codec.MarshalJSONIndent(types.ModuleCdc, coins) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error())) } diff --git a/x/distribution/keeper/querier.go b/x/distribution/keeper/querier.go index 27c657cb49a9..35058049f2a0 100644 --- a/x/distribution/keeper/querier.go +++ b/x/distribution/keeper/querier.go @@ -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 = make(sdk.DecCoins, 0) + } + bz, err := codec.MarshalJSONIndent(k.cdc, rewards) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error())) } @@ -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 = make(sdk.DecCoins, 0) + } bz, err := codec.MarshalJSONIndent(k.cdc, commission) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error())) @@ -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 = make(sdk.DecCoins, 0) + } bz, err := codec.MarshalJSONIndent(k.cdc, rewards) if err != nil { @@ -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 = make(sdk.DecCoins, 0) + } + bz, err := k.cdc.MarshalJSON(pool) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error())) } diff --git a/x/gov/keeper/querier.go b/x/gov/keeper/querier.go index 4ff43af6b973..5d9fd2a37409 100644 --- a/x/gov/keeper/querier.go +++ b/x/gov/keeper/querier.go @@ -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 = make(types.Deposits, 0) + } bz, err := codec.MarshalJSONIndent(keeper.cdc, deposits) if err != nil { @@ -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 = make(types.Votes, 0) + } bz, err := codec.MarshalJSONIndent(keeper.cdc, votes) if err != nil { @@ -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 = make([]types.Proposal, 0) + } + + bz, err := codec.MarshalJSONIndent(keeper.cdc, proposals) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error())) } diff --git a/x/staking/keeper/querier.go b/x/staking/keeper/querier.go index 3e3ced157c23..98086b326a79 100644 --- a/x/staking/keeper/querier.go +++ b/x/staking/keeper/querier.go @@ -115,6 +115,10 @@ func queryValidatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper) return nil, sdk.ErrInternal(err.Error()) } + if delegationResps == nil { + delegationResps = make(types.DelegationResponses, 0) + } + res, err := codec.MarshalJSONIndent(types.ModuleCdc, delegationResps) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", err.Error())) @@ -132,6 +136,9 @@ func queryValidatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery, } unbonds := k.GetUnbondingDelegationsFromValidator(ctx, params.ValidatorAddr) + if unbonds == nil { + unbonds = make(types.UnbondingDelegations, 0) + } res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbonds) if err != nil { @@ -155,6 +162,10 @@ func queryDelegatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper) return nil, sdk.ErrInternal(err.Error()) } + if delegationResps == nil { + delegationResps = make(types.DelegationResponses, 0) + } + res, err := codec.MarshalJSONIndent(types.ModuleCdc, delegationResps) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", err.Error())) @@ -172,6 +183,9 @@ func queryDelegatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery, } unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr) + if unbondingDelegations == nil { + unbondingDelegations = make(types.UnbondingDelegations, 0) + } res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbondingDelegations) if err != nil { @@ -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 = make(types.Validators, 0) + } res, err := codec.MarshalJSONIndent(types.ModuleCdc, validators) if err != nil { @@ -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 = make(types.RedelegationResponses, 0) + } + res, err := codec.MarshalJSONIndent(types.ModuleCdc, redelResponses) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", err.Error())) From 93bd409f475b6c0414008471a07cfa37cc147b2c Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 25 Oct 2019 21:53:55 +0200 Subject: [PATCH 2/7] Update x/bank/internal/keeper/querier.go Thanks, forgot about it! Co-Authored-By: Alessio Treglia --- x/bank/internal/keeper/querier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/bank/internal/keeper/querier.go b/x/bank/internal/keeper/querier.go index f7622d40deb9..bf303e2930fa 100644 --- a/x/bank/internal/keeper/querier.go +++ b/x/bank/internal/keeper/querier.go @@ -39,7 +39,7 @@ func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk coins := k.GetCoins(ctx, params.Address) if coins == nil { - coins = make(sdk.Coins, 0) + coins = sdk.NewCoins() } bz, err := codec.MarshalJSONIndent(types.ModuleCdc, coins) From 0303d2722301ed4d37d01806f660e24be3462db7 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 25 Oct 2019 21:54:33 +0200 Subject: [PATCH 3/7] Update x/distribution/keeper/querier.go Co-Authored-By: Alessio Treglia --- x/distribution/keeper/querier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/keeper/querier.go b/x/distribution/keeper/querier.go index 35058049f2a0..3f82e4c30547 100644 --- a/x/distribution/keeper/querier.go +++ b/x/distribution/keeper/querier.go @@ -87,7 +87,7 @@ func queryValidatorOutstandingRewards(ctx sdk.Context, path []string, req abci.R } rewards := k.GetValidatorOutstandingRewards(ctx, params.ValidatorAddress) if rewards == nil { - rewards = make(sdk.DecCoins, 0) + rewards = sdk.DecCoins{} } bz, err := codec.MarshalJSONIndent(k.cdc, rewards) if err != nil { From 8b4c4142f660ccb81a7d2b1bea56968e716c2ed1 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 25 Oct 2019 21:54:56 +0200 Subject: [PATCH 4/7] Update x/distribution/keeper/querier.go Co-Authored-By: Alessio Treglia --- x/distribution/keeper/querier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/keeper/querier.go b/x/distribution/keeper/querier.go index 3f82e4c30547..3a8e5bf4baa0 100644 --- a/x/distribution/keeper/querier.go +++ b/x/distribution/keeper/querier.go @@ -104,7 +104,7 @@ func queryValidatorCommission(ctx sdk.Context, path []string, req abci.RequestQu } commission := k.GetValidatorAccumulatedCommission(ctx, params.ValidatorAddress) if commission == nil { - commission = make(sdk.DecCoins, 0) + commission = sdk.DecCoins{} } bz, err := codec.MarshalJSONIndent(k.cdc, commission) if err != nil { From 4e9816a9e7daacb54b4cdb7038a23cd5af0e261b Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 25 Oct 2019 21:56:57 +0200 Subject: [PATCH 5/7] Update x/staking/keeper/querier.go Co-Authored-By: Alessio Treglia --- x/staking/keeper/querier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/keeper/querier.go b/x/staking/keeper/querier.go index 98086b326a79..5aa6de21f13f 100644 --- a/x/staking/keeper/querier.go +++ b/x/staking/keeper/querier.go @@ -116,7 +116,7 @@ func queryValidatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper) } if delegationResps == nil { - delegationResps = make(types.DelegationResponses, 0) + delegationResps = types.DelegationResponses{} } res, err := codec.MarshalJSONIndent(types.ModuleCdc, delegationResps) From a77982f35d91fcd267bc23cac15015b13f013752 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Fri, 25 Oct 2019 22:35:24 +0200 Subject: [PATCH 6/7] Removed make methods in favor on constructors --- x/distribution/keeper/querier.go | 4 ++-- x/gov/keeper/querier.go | 6 +++--- x/staking/keeper/querier.go | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/x/distribution/keeper/querier.go b/x/distribution/keeper/querier.go index 3a8e5bf4baa0..a057340340db 100644 --- a/x/distribution/keeper/querier.go +++ b/x/distribution/keeper/querier.go @@ -158,7 +158,7 @@ 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 = make(sdk.DecCoins, 0) + rewards = sdk.DecCoins{} } bz, err := codec.MarshalJSONIndent(k.cdc, rewards) @@ -254,7 +254,7 @@ func queryDelegatorWithdrawAddress(ctx sdk.Context, _ []string, req abci.Request func queryCommunityPool(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) { pool := k.GetFeePoolCommunityCoins(ctx) if pool == nil { - pool = make(sdk.DecCoins, 0) + pool = sdk.DecCoins{} } bz, err := k.cdc.MarshalJSON(pool) if err != nil { diff --git a/x/gov/keeper/querier.go b/x/gov/keeper/querier.go index 5d9fd2a37409..b27f8fa54e5b 100644 --- a/x/gov/keeper/querier.go +++ b/x/gov/keeper/querier.go @@ -131,7 +131,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper deposits := keeper.GetDeposits(ctx, params.ProposalID) if deposits == nil { - deposits = make(types.Deposits, 0) + deposits = types.Deposits{} } bz, err := codec.MarshalJSONIndent(keeper.cdc, deposits) @@ -186,7 +186,7 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke votes := keeper.GetVotes(ctx, params.ProposalID) if votes == nil { - votes = make(types.Votes, 0) + votes = types.Votes{} } bz, err := codec.MarshalJSONIndent(keeper.cdc, votes) @@ -206,7 +206,7 @@ func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper K proposals := keeper.GetProposalsFiltered(ctx, params) if proposals == nil { - proposals = make([]types.Proposal, 0) + proposals = types.Proposals{} } bz, err := codec.MarshalJSONIndent(keeper.cdc, proposals) diff --git a/x/staking/keeper/querier.go b/x/staking/keeper/querier.go index 5aa6de21f13f..a7e2ab6df751 100644 --- a/x/staking/keeper/querier.go +++ b/x/staking/keeper/querier.go @@ -57,7 +57,7 @@ func queryValidators(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, } validators := k.GetAllValidators(ctx) - filteredVals := make([]types.Validator, 0, len(validators)) + filteredVals := types.Validators{} for _, val := range validators { if strings.EqualFold(val.GetStatus().String(), params.Status) { @@ -137,7 +137,7 @@ func queryValidatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery, unbonds := k.GetUnbondingDelegationsFromValidator(ctx, params.ValidatorAddr) if unbonds == nil { - unbonds = make(types.UnbondingDelegations, 0) + unbonds = types.UnbondingDelegations{} } res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbonds) @@ -163,7 +163,7 @@ func queryDelegatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper) } if delegationResps == nil { - delegationResps = make(types.DelegationResponses, 0) + delegationResps = types.DelegationResponses{} } res, err := codec.MarshalJSONIndent(types.ModuleCdc, delegationResps) @@ -184,7 +184,7 @@ func queryDelegatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery, unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr) if unbondingDelegations == nil { - unbondingDelegations = make(types.UnbondingDelegations, 0) + unbondingDelegations = types.UnbondingDelegations{} } res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbondingDelegations) @@ -207,7 +207,7 @@ func queryDelegatorValidators(ctx sdk.Context, req abci.RequestQuery, k Keeper) validators := k.GetDelegatorValidators(ctx, params.DelegatorAddr, stakingParams.MaxValidators) if validators == nil { - validators = make(types.Validators, 0) + validators = types.Validators{} } res, err := codec.MarshalJSONIndent(types.ModuleCdc, validators) @@ -316,7 +316,7 @@ func queryRedelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byt } if redelResponses == nil { - redelResponses = make(types.RedelegationResponses, 0) + redelResponses = types.RedelegationResponses{} } res, err := codec.MarshalJSONIndent(types.ModuleCdc, redelResponses) From 5cc9c2814c3c0555fce4095351cfbd92b7c72b1f Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Sun, 27 Oct 2019 18:06:37 +0100 Subject: [PATCH 7/7] Undo change in filtered validators --- x/staking/keeper/querier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/keeper/querier.go b/x/staking/keeper/querier.go index a7e2ab6df751..d97bb07b10ef 100644 --- a/x/staking/keeper/querier.go +++ b/x/staking/keeper/querier.go @@ -57,7 +57,7 @@ func queryValidators(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, } validators := k.GetAllValidators(ctx) - filteredVals := types.Validators{} + filteredVals := make([]types.Validator, 0, len(validators)) for _, val := range validators { if strings.EqualFold(val.GetStatus().String(), params.Status) {