refactor: staking module using mocks (#12827)
## Description Closes: #12504 --- ### 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... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] 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)
This commit is contained in:
@@ -2,189 +2,49 @@ package keeper_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
func TestCancelUnbondingDelegation(t *testing.T) {
|
||||
// setup the app
|
||||
app := simapp.Setup(t, false)
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
msgServer := keeper.NewMsgServerImpl(app.StakingKeeper)
|
||||
bondDenom := app.StakingKeeper.BondDenom(ctx)
|
||||
|
||||
// set the not bonded pool module account
|
||||
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)
|
||||
startTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 5)
|
||||
|
||||
require.NoError(t, testutil.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), startTokens))))
|
||||
app.AccountKeeper.SetModuleAccount(ctx, notBondedPool)
|
||||
|
||||
moduleBalance := app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), app.StakingKeeper.BondDenom(ctx))
|
||||
require.Equal(t, sdk.NewInt64Coin(bondDenom, startTokens.Int64()), moduleBalance)
|
||||
|
||||
// accounts
|
||||
delAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(10000))
|
||||
validators := app.StakingKeeper.GetValidators(ctx, 10)
|
||||
require.Equal(t, len(validators), 1)
|
||||
|
||||
validatorAddr, err := sdk.ValAddressFromBech32(validators[0].OperatorAddress)
|
||||
require.NoError(t, err)
|
||||
delegatorAddr := delAddrs[0]
|
||||
|
||||
// setting the ubd entry
|
||||
unbondingAmount := sdk.NewInt64Coin(app.StakingKeeper.BondDenom(ctx), 5)
|
||||
ubd := types.NewUnbondingDelegation(
|
||||
delegatorAddr, validatorAddr, 10,
|
||||
ctx.BlockTime().Add(time.Minute*10),
|
||||
unbondingAmount.Amount,
|
||||
)
|
||||
|
||||
// set and retrieve a record
|
||||
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
|
||||
resUnbond, found := app.StakingKeeper.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr)
|
||||
require.True(t, found)
|
||||
require.Equal(t, ubd, resUnbond)
|
||||
|
||||
testCases := []struct {
|
||||
Name string
|
||||
ExceptErr bool
|
||||
req types.MsgCancelUnbondingDelegation
|
||||
}{
|
||||
{
|
||||
Name: "invalid height",
|
||||
ExceptErr: true,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: resUnbond.DelegatorAddress,
|
||||
ValidatorAddress: resUnbond.ValidatorAddress,
|
||||
Amount: sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(4)),
|
||||
CreationHeight: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "invalid coin",
|
||||
ExceptErr: true,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: resUnbond.DelegatorAddress,
|
||||
ValidatorAddress: resUnbond.ValidatorAddress,
|
||||
Amount: sdk.NewCoin("dump_coin", sdk.NewInt(4)),
|
||||
CreationHeight: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "validator not exists",
|
||||
ExceptErr: true,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: resUnbond.DelegatorAddress,
|
||||
ValidatorAddress: sdk.ValAddress(sdk.AccAddress("asdsad")).String(),
|
||||
Amount: unbondingAmount,
|
||||
CreationHeight: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "invalid delegator address",
|
||||
ExceptErr: true,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: "invalid_delegator_addrtess",
|
||||
ValidatorAddress: resUnbond.ValidatorAddress,
|
||||
Amount: unbondingAmount,
|
||||
CreationHeight: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "invalid amount",
|
||||
ExceptErr: true,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: resUnbond.DelegatorAddress,
|
||||
ValidatorAddress: resUnbond.ValidatorAddress,
|
||||
Amount: unbondingAmount.Add(sdk.NewInt64Coin(bondDenom, 10)),
|
||||
CreationHeight: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "success",
|
||||
ExceptErr: false,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: resUnbond.DelegatorAddress,
|
||||
ValidatorAddress: resUnbond.ValidatorAddress,
|
||||
Amount: unbondingAmount.Sub(sdk.NewInt64Coin(bondDenom, 1)),
|
||||
CreationHeight: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "success",
|
||||
ExceptErr: false,
|
||||
req: types.MsgCancelUnbondingDelegation{
|
||||
DelegatorAddress: resUnbond.DelegatorAddress,
|
||||
ValidatorAddress: resUnbond.ValidatorAddress,
|
||||
Amount: unbondingAmount.Sub(unbondingAmount.Sub(sdk.NewInt64Coin(bondDenom, 1))),
|
||||
CreationHeight: 10,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.Name, func(t *testing.T) {
|
||||
_, err := msgServer.CancelUnbondingDelegation(ctx, &testCase.req)
|
||||
if testCase.ExceptErr {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
balanceForNotBondedPool := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(notBondedPool.GetAddress()), bondDenom)
|
||||
require.Equal(t, balanceForNotBondedPool, moduleBalance.Sub(testCase.req.Amount))
|
||||
moduleBalance = moduleBalance.Sub(testCase.req.Amount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMsgUpdateParams(t *testing.T) {
|
||||
app := simapp.Setup(t, false)
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
msgServer := keeper.NewMsgServerImpl(app.StakingKeeper)
|
||||
func (s *KeeperTestSuite) TestMsgUpdateParams() {
|
||||
ctx, keeper, msgServer := s.ctx, s.stakingKeeper, s.msgServer
|
||||
require := s.Require()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input *types.MsgUpdateParams
|
||||
input *stakingtypes.MsgUpdateParams
|
||||
expErr bool
|
||||
expErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "valid params",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: app.StakingKeeper.GetAuthority(),
|
||||
Params: types.DefaultParams(),
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: keeper.GetAuthority(),
|
||||
Params: stakingtypes.DefaultParams(),
|
||||
},
|
||||
expErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid authority",
|
||||
input: &types.MsgUpdateParams{
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: "invalid",
|
||||
Params: types.DefaultParams(),
|
||||
Params: stakingtypes.DefaultParams(),
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "invalid authority",
|
||||
},
|
||||
{
|
||||
name: "negative commission rate",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: app.StakingKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: keeper.GetAuthority(),
|
||||
Params: stakingtypes.Params{
|
||||
MinCommissionRate: math.LegacyNewDec(-10),
|
||||
UnbondingTime: types.DefaultUnbondingTime,
|
||||
MaxValidators: types.DefaultMaxValidators,
|
||||
MaxEntries: types.DefaultMaxEntries,
|
||||
HistoricalEntries: types.DefaultHistoricalEntries,
|
||||
BondDenom: types.BondStatusBonded,
|
||||
UnbondingTime: stakingtypes.DefaultUnbondingTime,
|
||||
MaxValidators: stakingtypes.DefaultMaxValidators,
|
||||
MaxEntries: stakingtypes.DefaultMaxEntries,
|
||||
HistoricalEntries: stakingtypes.DefaultHistoricalEntries,
|
||||
BondDenom: stakingtypes.BondStatusBonded,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
@@ -192,15 +52,15 @@ func TestMsgUpdateParams(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "commission rate cannot be bigger than 100",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: app.StakingKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: keeper.GetAuthority(),
|
||||
Params: stakingtypes.Params{
|
||||
MinCommissionRate: math.LegacyNewDec(2),
|
||||
UnbondingTime: types.DefaultUnbondingTime,
|
||||
MaxValidators: types.DefaultMaxValidators,
|
||||
MaxEntries: types.DefaultMaxEntries,
|
||||
HistoricalEntries: types.DefaultHistoricalEntries,
|
||||
BondDenom: types.BondStatusBonded,
|
||||
UnbondingTime: stakingtypes.DefaultUnbondingTime,
|
||||
MaxValidators: stakingtypes.DefaultMaxValidators,
|
||||
MaxEntries: stakingtypes.DefaultMaxEntries,
|
||||
HistoricalEntries: stakingtypes.DefaultHistoricalEntries,
|
||||
BondDenom: stakingtypes.BondStatusBonded,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
@@ -208,14 +68,14 @@ func TestMsgUpdateParams(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "invalid bond denom",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: app.StakingKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
MinCommissionRate: types.DefaultMinCommissionRate,
|
||||
UnbondingTime: types.DefaultUnbondingTime,
|
||||
MaxValidators: types.DefaultMaxValidators,
|
||||
MaxEntries: types.DefaultMaxEntries,
|
||||
HistoricalEntries: types.DefaultHistoricalEntries,
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: keeper.GetAuthority(),
|
||||
Params: stakingtypes.Params{
|
||||
MinCommissionRate: stakingtypes.DefaultMinCommissionRate,
|
||||
UnbondingTime: stakingtypes.DefaultUnbondingTime,
|
||||
MaxValidators: stakingtypes.DefaultMaxValidators,
|
||||
MaxEntries: stakingtypes.DefaultMaxEntries,
|
||||
HistoricalEntries: stakingtypes.DefaultHistoricalEntries,
|
||||
BondDenom: "",
|
||||
},
|
||||
},
|
||||
@@ -224,15 +84,15 @@ func TestMsgUpdateParams(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "max validators most be positive",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: app.StakingKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
MinCommissionRate: types.DefaultMinCommissionRate,
|
||||
UnbondingTime: types.DefaultUnbondingTime,
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: keeper.GetAuthority(),
|
||||
Params: stakingtypes.Params{
|
||||
MinCommissionRate: stakingtypes.DefaultMinCommissionRate,
|
||||
UnbondingTime: stakingtypes.DefaultUnbondingTime,
|
||||
MaxValidators: 0,
|
||||
MaxEntries: types.DefaultMaxEntries,
|
||||
HistoricalEntries: types.DefaultHistoricalEntries,
|
||||
BondDenom: types.BondStatusBonded,
|
||||
MaxEntries: stakingtypes.DefaultMaxEntries,
|
||||
HistoricalEntries: stakingtypes.DefaultHistoricalEntries,
|
||||
BondDenom: stakingtypes.BondStatusBonded,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
@@ -240,15 +100,15 @@ func TestMsgUpdateParams(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "max entries most be positive",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: app.StakingKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
MinCommissionRate: types.DefaultMinCommissionRate,
|
||||
UnbondingTime: types.DefaultUnbondingTime,
|
||||
MaxValidators: types.DefaultMaxValidators,
|
||||
input: &stakingtypes.MsgUpdateParams{
|
||||
Authority: keeper.GetAuthority(),
|
||||
Params: stakingtypes.Params{
|
||||
MinCommissionRate: stakingtypes.DefaultMinCommissionRate,
|
||||
UnbondingTime: stakingtypes.DefaultUnbondingTime,
|
||||
MaxValidators: stakingtypes.DefaultMaxValidators,
|
||||
MaxEntries: 0,
|
||||
HistoricalEntries: types.DefaultHistoricalEntries,
|
||||
BondDenom: types.BondStatusBonded,
|
||||
HistoricalEntries: stakingtypes.DefaultHistoricalEntries,
|
||||
BondDenom: stakingtypes.BondStatusBonded,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
@@ -258,13 +118,13 @@ func TestMsgUpdateParams(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s.T().Run(tc.name, func(t *testing.T) {
|
||||
_, err := msgServer.UpdateParams(ctx, tc.input)
|
||||
if tc.expErr {
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), tc.expErrMsg)
|
||||
require.Error(err)
|
||||
require.Contains(err.Error(), tc.expErrMsg)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user