2019-01-18 11:38:40 -08:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2019-06-28 13:11:27 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2019-01-18 11:38:40 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const custom = "custom"
|
|
|
|
|
2020-01-10 12:08:14 -08:00
|
|
|
func getQueriedParams(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier) types.Params {
|
|
|
|
var params types.Params
|
2019-01-18 11:38:40 -08:00
|
|
|
|
2020-01-10 12:08:14 -08:00
|
|
|
bz, err := querier(ctx, []string{types.QueryParams}, abci.RequestQuery{})
|
2019-01-18 11:38:40 -08:00
|
|
|
require.Nil(t, err)
|
2020-01-10 12:08:14 -08:00
|
|
|
require.Nil(t, cdc.UnmarshalJSON(bz, ¶ms))
|
2019-01-18 11:38:40 -08:00
|
|
|
|
2020-01-10 12:08:14 -08:00
|
|
|
return params
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-06 10:54:12 -08:00
|
|
|
func getQueriedValidatorOutstandingRewards(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, validatorAddr sdk.ValAddress) (outstandingRewards sdk.DecCoins) {
|
2019-01-18 11:38:40 -08:00
|
|
|
query := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryValidatorOutstandingRewards}, "/"),
|
|
|
|
Data: cdc.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr)),
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
bz, err := querier(ctx, []string{types.QueryValidatorOutstandingRewards}, query)
|
2019-01-18 11:38:40 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, cdc.UnmarshalJSON(bz, &outstandingRewards))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getQueriedValidatorCommission(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, validatorAddr sdk.ValAddress) (validatorCommission sdk.DecCoins) {
|
|
|
|
query := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryValidatorCommission}, "/"),
|
|
|
|
Data: cdc.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
bz, err := querier(ctx, []string{types.QueryValidatorCommission}, query)
|
2019-01-18 11:38:40 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, cdc.UnmarshalJSON(bz, &validatorCommission))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getQueriedValidatorSlashes(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, validatorAddr sdk.ValAddress, startHeight uint64, endHeight uint64) (slashes []types.ValidatorSlashEvent) {
|
|
|
|
query := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryValidatorSlashes}, "/"),
|
|
|
|
Data: cdc.MustMarshalJSON(types.NewQueryValidatorSlashesParams(validatorAddr, startHeight, endHeight)),
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
bz, err := querier(ctx, []string{types.QueryValidatorSlashes}, query)
|
2019-01-18 11:38:40 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, cdc.UnmarshalJSON(bz, &slashes))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getQueriedDelegationRewards(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) (rewards sdk.DecCoins) {
|
|
|
|
query := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryDelegationRewards}, "/"),
|
|
|
|
Data: cdc.MustMarshalJSON(types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)),
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
bz, err := querier(ctx, []string{types.QueryDelegationRewards}, query)
|
2019-01-18 11:38:40 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, cdc.UnmarshalJSON(bz, &rewards))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
func getQueriedDelegatorTotalRewards(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, delegatorAddr sdk.AccAddress) (response types.QueryDelegatorTotalRewardsResponse) {
|
|
|
|
query := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryDelegatorTotalRewards}, "/"),
|
|
|
|
Data: cdc.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
|
2019-04-16 06:43:08 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
bz, err := querier(ctx, []string{types.QueryDelegatorTotalRewards}, query)
|
2019-04-16 06:43:08 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, cdc.UnmarshalJSON(bz, &response))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:05:36 -07:00
|
|
|
func getQueriedCommunityPool(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier) (ptr []byte) {
|
|
|
|
query := abci.RequestQuery{
|
2019-06-05 16:26:17 -07:00
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryCommunityPool}, ""),
|
2019-03-27 18:05:36 -07:00
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
cp, err := querier(ctx, []string{types.QueryCommunityPool}, query)
|
2019-03-27 18:05:36 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, cdc.UnmarshalJSON(cp, &ptr))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
func TestQueries(t *testing.T) {
|
|
|
|
cdc := codec.New()
|
2019-04-16 06:43:08 -07:00
|
|
|
types.RegisterCodec(cdc)
|
2019-06-28 13:11:27 -07:00
|
|
|
supply.RegisterCodec(cdc)
|
2020-01-30 13:31:16 -08:00
|
|
|
ctx, _, _, keeper, sk, _ := CreateTestInputDefault(t, false, 100)
|
2019-01-18 11:38:40 -08:00
|
|
|
querier := NewQuerier(keeper)
|
|
|
|
|
|
|
|
// test param queries
|
2020-01-10 12:08:14 -08:00
|
|
|
params := types.Params{
|
|
|
|
CommunityTax: sdk.NewDecWithPrec(3, 1),
|
|
|
|
BaseProposerReward: sdk.NewDecWithPrec(2, 1),
|
|
|
|
BonusProposerReward: sdk.NewDecWithPrec(1, 1),
|
|
|
|
WithdrawAddrEnabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
keeper.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)
|
2019-01-18 11:38:40 -08:00
|
|
|
|
|
|
|
// test outstanding rewards query
|
2019-09-16 10:11:17 -07:00
|
|
|
outstandingRewards := sdk.DecCoins{{Denom: "mytoken", Amount: sdk.NewDec(3)}, {Denom: "myothertoken", Amount: sdk.NewDecWithPrec(3, 7)}}
|
2019-03-06 10:54:12 -08:00
|
|
|
keeper.SetValidatorOutstandingRewards(ctx, valOpAddr1, outstandingRewards)
|
|
|
|
retOutstandingRewards := getQueriedValidatorOutstandingRewards(t, ctx, cdc, querier, valOpAddr1)
|
2019-01-18 11:38:40 -08:00
|
|
|
require.Equal(t, outstandingRewards, retOutstandingRewards)
|
|
|
|
|
|
|
|
// test validator commission query
|
2019-09-16 10:11:17 -07:00
|
|
|
commission := sdk.DecCoins{{Denom: "token1", Amount: sdk.NewDec(4)}, {Denom: "token2", Amount: sdk.NewDec(2)}}
|
2019-01-18 11:38:40 -08:00
|
|
|
keeper.SetValidatorAccumulatedCommission(ctx, valOpAddr1, commission)
|
|
|
|
retCommission := getQueriedValidatorCommission(t, ctx, cdc, querier, valOpAddr1)
|
|
|
|
require.Equal(t, commission, retCommission)
|
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
// test delegator's total rewards query
|
|
|
|
delRewards := getQueriedDelegatorTotalRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1))
|
|
|
|
require.Equal(t, types.QueryDelegatorTotalRewardsResponse{}, delRewards)
|
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
// test validator slashes query with height range
|
|
|
|
slashOne := types.NewValidatorSlashEvent(3, sdk.NewDecWithPrec(5, 1))
|
|
|
|
slashTwo := types.NewValidatorSlashEvent(7, sdk.NewDecWithPrec(6, 1))
|
2019-07-01 09:03:34 -07:00
|
|
|
keeper.SetValidatorSlashEvent(ctx, valOpAddr1, 3, 0, slashOne)
|
|
|
|
keeper.SetValidatorSlashEvent(ctx, valOpAddr1, 7, 0, slashTwo)
|
2019-01-18 11:38:40 -08:00
|
|
|
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
|
|
|
|
sh := staking.NewHandler(sk)
|
2019-06-03 14:14:21 -07:00
|
|
|
comm := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
|
2019-12-27 09:57:54 -08:00
|
|
|
msg := staking.NewMsgCreateValidator(
|
|
|
|
valOpAddr1, valConsPk1, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), staking.Description{}, comm, sdk.OneInt(),
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := sh(ctx, msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
staking.EndBlocker(ctx, sk)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
val := sk.Validator(ctx, valOpAddr1)
|
|
|
|
rewards := getQueriedDelegationRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1), valOpAddr1)
|
|
|
|
require.True(t, rewards.IsZero())
|
|
|
|
initial := int64(10)
|
2019-03-06 10:54:12 -08:00
|
|
|
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
2019-09-16 10:11:17 -07:00
|
|
|
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(initial)}}
|
2019-01-18 11:38:40 -08:00
|
|
|
keeper.AllocateTokensToValidator(ctx, val, tokens)
|
|
|
|
rewards = getQueriedDelegationRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1), valOpAddr1)
|
2019-09-16 10:11:17 -07:00
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(initial / 2)}}, rewards)
|
2019-03-27 18:05:36 -07:00
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
// 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)
|
|
|
|
|
2019-03-27 18:05:36 -07:00
|
|
|
// currently community pool hold nothing so we should return null
|
|
|
|
communityPool := getQueriedCommunityPool(t, ctx, cdc, querier)
|
|
|
|
require.Nil(t, communityPool)
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|