Skip to content

Commit

Permalink
refactor(tests/integration/distribution): using setupValidatorWithCom…
Browse files Browse the repository at this point in the history
…mission helper function (#20275)
  • Loading branch information
kien6034 authored May 6, 2024
1 parent e901584 commit fd5c2f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 74 deletions.
25 changes: 25 additions & 0 deletions tests/integration/distribution/keeper/common_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
package keeper_test

import (
"testing"

"gotest.tools/v3/assert"

"cosmossdk.io/math"
"cosmossdk.io/x/distribution/types"
stakingtestutil "cosmossdk.io/x/staking/testutil"
stakingtypes "cosmossdk.io/x/staking/types"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
PKS = simtestutil.CreateTestPubKeys(3)

valConsPk0 = PKS[0]
)

func setupValidatorWithCommission(t *testing.T, f *fixture, valAddr sdk.ValAddress, initialStake int64) {
t.Helper()
initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))))
f.accountKeeper.SetAccount(f.sdkCtx, f.accountKeeper.NewAccountWithAddress(f.sdkCtx, sdk.AccAddress(valAddr)))

tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0))
tstaking.CreateValidator(valAddr, valConsPk0, math.NewInt(initialStake), true)
}
90 changes: 16 additions & 74 deletions tests/integration/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/math"
"cosmossdk.io/x/distribution/types"
stakingtestutil "cosmossdk.io/x/staking/testutil"
stakingtypes "cosmossdk.io/x/staking/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -22,9 +21,6 @@ func TestGRPCParams(t *testing.T) {

assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

var (
params types.Params
expParams types.Params
Expand Down Expand Up @@ -64,7 +60,7 @@ func TestGRPCParams(t *testing.T) {
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
tc.malleate()

paramsRes, err := queryClient.Params(f.sdkCtx, tc.msg)
paramsRes, err := f.queryClient.Params(f.sdkCtx, tc.msg)
assert.NilError(t, err)
assert.Assert(t, paramsRes != nil)
assert.DeepEqual(t, paramsRes.Params, expParams)
Expand All @@ -77,30 +73,14 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) {
t.Parallel()
f := initFixture(t)

// set module account coins
initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))

// Set default staking params
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)
assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams()))
setupValidatorWithCommission(t, f, f.valAddr, 10) // Setup a validator with commission

valCommission := sdk.DecCoins{
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(5000)),
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(300)),
}

// send funds to val addr
funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))))
f.accountKeeper.SetAccount(f.sdkCtx, f.accountKeeper.NewAccountWithAddress(f.sdkCtx, sdk.AccAddress(f.valAddr)))
initialStake := int64(10)
tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0))
tstaking.CreateValidator(f.valAddr, valConsPk0, math.NewInt(initialStake), true)

// set outstanding rewards
err := f.distrKeeper.ValidatorOutstandingRewards.Set(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission})
assert.NilError(t, err)
Expand Down Expand Up @@ -136,7 +116,7 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
validatorOutstandingRewards, err := queryClient.ValidatorOutstandingRewards(f.sdkCtx, tc.msg)
validatorOutstandingRewards, err := f.queryClient.ValidatorOutstandingRewards(f.sdkCtx, tc.msg)

if tc.expPass {
assert.NilError(t, err)
Expand All @@ -154,24 +134,8 @@ func TestGRPCValidatorCommission(t *testing.T) {
t.Parallel()
f := initFixture(t)

// set module account coins
initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))

// Set default staking params
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

// send funds to val addr
funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))))
f.accountKeeper.SetAccount(f.sdkCtx, f.accountKeeper.NewAccountWithAddress(f.sdkCtx, sdk.AccAddress(f.valAddr)))
initialStake := int64(10)
tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0))
tstaking.CreateValidator(f.valAddr, valConsPk0, math.NewInt(initialStake), true)
assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams())) // Set default distribution parameters
setupValidatorWithCommission(t, f, f.valAddr, 10) // Setup a validator with commission

commission := sdk.DecCoins{sdk.DecCoin{Denom: "token1", Amount: math.LegacyNewDec(4)}, {Denom: "token2", Amount: math.LegacyNewDec(2)}}
assert.NilError(t, f.distrKeeper.ValidatorsAccumulatedCommission.Set(f.sdkCtx, f.valAddr, types.ValidatorAccumulatedCommission{Commission: commission}))
Expand Down Expand Up @@ -204,7 +168,7 @@ func TestGRPCValidatorCommission(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
commissionRes, err := queryClient.ValidatorCommission(f.sdkCtx, tc.msg)
commissionRes, err := f.queryClient.ValidatorCommission(f.sdkCtx, tc.msg)

if tc.expPass {
assert.NilError(t, err)
Expand All @@ -222,9 +186,6 @@ func TestGRPCValidatorSlashes(t *testing.T) {
t.Parallel()
f := initFixture(t)

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

addr2 := sdk.AccAddress(PKS[1].Address())
valAddr2 := sdk.ValAddress(addr2)

Expand Down Expand Up @@ -359,7 +320,7 @@ func TestGRPCValidatorSlashes(t *testing.T) {
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
tc.malleate()

slashesRes, err := queryClient.ValidatorSlashes(f.sdkCtx, req)
slashesRes, err := f.queryClient.ValidatorSlashes(f.sdkCtx, req)

if tc.expPass {
assert.NilError(t, err)
Expand All @@ -378,9 +339,6 @@ func TestGRPCDelegatorWithdrawAddress(t *testing.T) {

assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

addr2 := sdk.AccAddress(PKS[1].Address())

err := f.distrKeeper.SetWithdrawAddr(f.sdkCtx, f.addr, addr2)
Expand Down Expand Up @@ -408,7 +366,7 @@ func TestGRPCDelegatorWithdrawAddress(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
withdrawAddress, err := queryClient.DelegatorWithdrawAddress(f.sdkCtx, tc.msg)
withdrawAddress, err := f.queryClient.DelegatorWithdrawAddress(f.sdkCtx, tc.msg)

if tc.expPass {
assert.NilError(t, err)
Expand All @@ -425,9 +383,6 @@ func TestGRPCCommunityPool(t *testing.T) {
t.Parallel()
f := initFixture(t)

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

var (
req *types.QueryCommunityPoolRequest //nolint:staticcheck // we're using a deprecated call
expPool *types.QueryCommunityPoolResponse //nolint:staticcheck // we're using a deprecated call
Expand Down Expand Up @@ -465,7 +420,7 @@ func TestGRPCCommunityPool(t *testing.T) {
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
testCase.malleate()

pool, err := queryClient.CommunityPool(f.sdkCtx, req) //nolint:staticcheck // we're using a deprecated call
pool, err := f.queryClient.CommunityPool(f.sdkCtx, req) //nolint:staticcheck // we're using a deprecated call

assert.NilError(t, err)
assert.DeepEqual(t, expPool, pool)
Expand All @@ -481,32 +436,19 @@ func TestGRPCDelegationRewards(t *testing.T) {
CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(1000)}),
}))

// set module account coins
initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))
initialStake := int64(10)
assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams()))
setupValidatorWithCommission(t, f, f.valAddr, initialStake) // Setup a validator with commission
val, found := f.stakingKeeper.GetValidator(f.sdkCtx, f.valAddr)
assert.Assert(t, found)

// Set default staking params
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

addr2 := sdk.AccAddress(PKS[1].Address())
valAddr2 := sdk.ValAddress(addr2)
delAddr := sdk.AccAddress(PKS[2].Address())

// send funds to val addr
funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000))
assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))))
f.accountKeeper.SetAccount(f.sdkCtx, f.accountKeeper.NewAccountWithAddress(f.sdkCtx, sdk.AccAddress(f.valAddr)))
initialStake := int64(10)
tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0))
tstaking.CreateValidator(f.valAddr, valConsPk0, math.NewInt(initialStake), true)

val, found := f.stakingKeeper.GetValidator(f.sdkCtx, f.valAddr)
assert.Assert(t, found)

// setup delegation
delTokens := sdk.TokensFromConsensusPower(2, sdk.DefaultPowerReduction)
validator, issuedShares := val.AddTokensFromDel(delTokens)
Expand Down Expand Up @@ -582,7 +524,7 @@ func TestGRPCDelegationRewards(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
rewards, err := queryClient.DelegationRewards(f.sdkCtx, tc.msg)
rewards, err := f.queryClient.DelegationRewards(f.sdkCtx, tc.msg)

if tc.expPass {
assert.NilError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type fixture struct {
cdc codec.Codec
keys map[string]*storetypes.KVStoreKey

queryClient distrtypes.QueryClient

accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
distrKeeper distrkeeper.Keeper
Expand Down Expand Up @@ -174,6 +176,9 @@ func initFixture(t *testing.T) *fixture {
distrtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), distrkeeper.NewMsgServerImpl(distrKeeper))
distrtypes.RegisterQueryServer(integrationApp.QueryHelper(), distrkeeper.NewQuerier(distrKeeper))

qr := integrationApp.QueryHelper()
distrQueryClient := distrtypes.NewQueryClient(qr)

return &fixture{
app: integrationApp,
sdkCtx: sdkCtx,
Expand All @@ -186,6 +191,7 @@ func initFixture(t *testing.T) *fixture {
poolKeeper: poolKeeper,
addr: addr,
valAddr: valAddr,
queryClient: distrQueryClient,
}
}

Expand Down

0 comments on commit fd5c2f7

Please sign in to comment.