diff --git a/CHANGELOG.md b/CHANGELOG.md index 113597cb7aa9..0d01901e725b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,8 @@ either the truncated coins or the change coins. * [\#3915](https://github.com/cosmos/cosmos-sdk/issues/3915) Remove ';' delimiting support from ParseDecCoins * [\#3977](https://github.com/cosmos/cosmos-sdk/issues/3977) Fix docker image build +* [\#4020](https://github.com/cosmos/cosmos-sdk/issues/4020) Fix queryDelegationRewards by returning an error +when the validator or delegation do not exist. ## 0.33.2 diff --git a/x/distribution/keeper/querier.go b/x/distribution/keeper/querier.go index ac40700c52f1..984bb744824a 100644 --- a/x/distribution/keeper/querier.go +++ b/x/distribution/keeper/querier.go @@ -207,7 +207,17 @@ func queryDelegationRewards(ctx sdk.Context, _ []string, req abci.RequestQuery, ctx, _ = ctx.CacheContext() val := k.stakingKeeper.Validator(ctx, params.ValidatorAddress) + if val == nil { + // TODO: Should use ErrNoValidatorFound from staking/types + return nil, sdk.ErrInternal(fmt.Sprintf("validator %s does not exist", params.ValidatorAddress)) + } + del := k.stakingKeeper.Delegation(ctx, params.DelegatorAddress, params.ValidatorAddress) + if del == nil { + // TODO: Should use ErrNoDelegation from staking/types + return nil, sdk.ErrInternal("delegation does not exist") + } + endingPeriod := k.incrementValidatorPeriod(ctx, val) rewards := k.calculateDelegationRewards(ctx, val, del, endingPeriod)