cosmos-sdk/x/distribution/keeper/querier_test.go

204 lines
8.7 KiB
Go
Raw Normal View History

package keeper_test
import (
"strings"
"testing"
2020-03-20 10:14:14 -07:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2020-03-20 10:14:14 -07:00
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
const custom = "custom"
func getQueriedParams(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier) types.Params {
var params types.Params
bz, err := querier(ctx, []string{types.QueryParams}, abci.RequestQuery{})
require.Nil(t, err)
require.Nil(t, cdc.UnmarshalJSON(bz, &params))
return params
}
func getQueriedValidatorOutstandingRewards(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, validatorAddr sdk.ValAddress) sdk.DecCoins {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryValidatorOutstandingRewards}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr)),
}
bz, err := querier(ctx, []string{types.QueryValidatorOutstandingRewards}, query)
require.Nil(t, err)
outstandingRewards := types.ValidatorOutstandingRewards{}
require.Nil(t, cdc.UnmarshalJSON(bz, &outstandingRewards))
return outstandingRewards.GetRewards()
}
func getQueriedValidatorCommission(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, validatorAddr sdk.ValAddress) sdk.DecCoins {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryValidatorCommission}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
}
bz, err := querier(ctx, []string{types.QueryValidatorCommission}, query)
require.Nil(t, err)
validatorCommission := types.ValidatorAccumulatedCommission{}
require.Nil(t, cdc.UnmarshalJSON(bz, &validatorCommission))
return validatorCommission.GetCommission()
}
func getQueriedValidatorSlashes(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, validatorAddr sdk.ValAddress, startHeight uint64, endHeight uint64) (slashes []types.ValidatorSlashEvent) {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryValidatorSlashes}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryValidatorSlashesParams(validatorAddr, startHeight, endHeight)),
}
bz, err := querier(ctx, []string{types.QueryValidatorSlashes}, query)
require.Nil(t, err)
require.Nil(t, cdc.UnmarshalJSON(bz, &slashes))
return
}
func getQueriedDelegationRewards(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) (rewards sdk.DecCoins) {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryDelegationRewards}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)),
}
bz, err := querier(ctx, []string{types.QueryDelegationRewards}, query)
require.Nil(t, err)
require.Nil(t, cdc.UnmarshalJSON(bz, &rewards))
return
}
func getQueriedDelegatorTotalRewards(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, delegatorAddr sdk.AccAddress) (response types.QueryDelegatorTotalRewardsResponse) {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryDelegatorTotalRewards}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
}
bz, err := querier(ctx, []string{types.QueryDelegatorTotalRewards}, query)
require.Nil(t, err)
require.Nil(t, cdc.UnmarshalJSON(bz, &response))
return
}
func getQueriedCommunityPool(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier) (ptr []byte) {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryCommunityPool}, ""),
Data: []byte{},
}
cp, err := querier(ctx, []string{types.QueryCommunityPool}, query)
require.Nil(t, err)
require.Nil(t, cdc.UnmarshalJSON(cp, &ptr))
return
}
func TestQueries(t *testing.T) {
cdc := codec.NewLegacyAmino()
types.RegisterLegacyAminoCodec(cdc)
banktypes.RegisterLegacyAminoCodec(cdc)
refactor(test)!: refactor `simapp.Setup` function (#9938) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description ref: #8961 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> Following up on [#9697](https://github.com/cosmos/cosmos-sdk/pull/9697#pullrequestreview-727295733), this PR is the first step for the #8961. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-08-16 17:52:06 -07:00
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
feat!: Ensure InitGenesis returns with non-empty validator set (#9697) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #8961 SDK allows InitGenesis to return with an empty validator set. In practice, the error for an empty validator set gets thrown in tendermint. To fix this, * Add non-empty validator set check to the `mm.InitGenesis` function. This will break `simapp.Setup` because it relies on an empty validator set [#comment](https://github.com/cosmos/cosmos-sdk/pull/8909#issuecomment-804850834). * Update `simapp.Setup` to use a single validator. * Fix failing tests (Most of them are keeper tests). <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-10-05 05:02:51 -07:00
// reset fee pool
app.DistrKeeper.SetFeePool(ctx, types.InitialFeePool())
addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
valOpAddr1 := valAddrs[0]
querier := keeper.NewQuerier(app.DistrKeeper, cdc)
// test param queries
params := types.Params{
CommunityTax: sdk.NewDecWithPrec(3, 1),
BaseProposerReward: sdk.NewDecWithPrec(2, 1),
BonusProposerReward: sdk.NewDecWithPrec(1, 1),
WithdrawAddrEnabled: true,
}
app.DistrKeeper.SetParams(ctx, params)
paramsRes := getQueriedParams(t, ctx, cdc, querier)
require.Equal(t, params.CommunityTax, paramsRes.CommunityTax)
require.Equal(t, params.BaseProposerReward, paramsRes.BaseProposerReward)
require.Equal(t, params.BonusProposerReward, paramsRes.BonusProposerReward)
require.Equal(t, params.WithdrawAddrEnabled, paramsRes.WithdrawAddrEnabled)
// test outstanding rewards query
outstandingRewards := sdk.DecCoins{{Denom: "mytoken", Amount: sdk.NewDec(3)}, {Denom: "myothertoken", Amount: sdk.NewDecWithPrec(3, 7)}}
app.DistrKeeper.SetValidatorOutstandingRewards(ctx, valOpAddr1, types.ValidatorOutstandingRewards{Rewards: outstandingRewards})
retOutstandingRewards := getQueriedValidatorOutstandingRewards(t, ctx, cdc, querier, valOpAddr1)
require.Equal(t, outstandingRewards, retOutstandingRewards)
// test validator commission query
commission := sdk.DecCoins{{Denom: "token1", Amount: sdk.NewDec(4)}, {Denom: "token2", Amount: sdk.NewDec(2)}}
app.DistrKeeper.SetValidatorAccumulatedCommission(ctx, valOpAddr1, types.ValidatorAccumulatedCommission{Commission: commission})
retCommission := getQueriedValidatorCommission(t, ctx, cdc, querier, valOpAddr1)
require.Equal(t, commission, retCommission)
// test delegator's total rewards query
delRewards := getQueriedDelegatorTotalRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1))
require.Equal(t, types.QueryDelegatorTotalRewardsResponse{}, delRewards)
// test validator slashes query with height range
slashOne := types.NewValidatorSlashEvent(3, sdk.NewDecWithPrec(5, 1))
slashTwo := types.NewValidatorSlashEvent(7, sdk.NewDecWithPrec(6, 1))
app.DistrKeeper.SetValidatorSlashEvent(ctx, valOpAddr1, 3, 0, slashOne)
app.DistrKeeper.SetValidatorSlashEvent(ctx, valOpAddr1, 7, 0, slashTwo)
slashes := getQueriedValidatorSlashes(t, ctx, cdc, querier, valOpAddr1, 0, 2)
require.Equal(t, 0, len(slashes))
slashes = getQueriedValidatorSlashes(t, ctx, cdc, querier, valOpAddr1, 0, 5)
require.Equal(t, []types.ValidatorSlashEvent{slashOne}, slashes)
slashes = getQueriedValidatorSlashes(t, ctx, cdc, querier, valOpAddr1, 0, 10)
require.Equal(t, []types.ValidatorSlashEvent{slashOne, slashTwo}, slashes)
// test delegation rewards query
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valOpAddr1, valConsPk1, sdk.NewInt(100), true)
staking.EndBlocker(ctx, app.StakingKeeper)
val := app.StakingKeeper.Validator(ctx, valOpAddr1)
rewards := getQueriedDelegationRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1), valOpAddr1)
require.True(t, rewards.IsZero())
initial := int64(10)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(initial)}}
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)
rewards = getQueriedDelegationRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1), valOpAddr1)
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(initial / 2)}}, rewards)
// test delegator's total rewards query
delRewards = getQueriedDelegatorTotalRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1))
expectedDelReward := types.NewDelegationDelegatorReward(valOpAddr1,
sdk.DecCoins{sdk.NewInt64DecCoin("stake", 5)})
wantDelRewards := types.NewQueryDelegatorTotalRewardsResponse(
[]types.DelegationDelegatorReward{expectedDelReward}, expectedDelReward.Reward)
require.Equal(t, wantDelRewards, delRewards)
// currently community pool hold nothing so we should return null
communityPool := getQueriedCommunityPool(t, ctx, cdc, querier)
require.Nil(t, communityPool)
}