Skip to content

Commit

Permalink
refactor(staking): add missing msgServer tests and move ValidateBasic…
Browse files Browse the repository at this point in the history
…() logic to msgServer (#15820)
  • Loading branch information
likhita-809 authored Apr 14, 2023
1 parent d7c8353 commit 7132a76
Show file tree
Hide file tree
Showing 13 changed files with 1,151 additions and 495 deletions.
1 change: 1 addition & 0 deletions tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestUnJailNotBonded(t *testing.T) {
amt := f.stakingKeeper.TokensFromConsensusPower(f.ctx, 50)
msg := tstaking.CreateValidatorMsg(addr, val, amt)
msg.MinSelfDelegation = amt
msg.Description = stakingtypes.Description{Moniker: "TestValidator"}
res, err := tstaking.CreateValidatorWithMsg(f.ctx, msg)
assert.NilError(t, err)
assert.Assert(t, res != nil)
Expand Down
53 changes: 32 additions & 21 deletions tests/integration/staking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,58 @@ func TestCancelUnbondingDelegation(t *testing.T) {
assert.DeepEqual(t, ubd, resUnbond)

testCases := []struct {
Name string
ExceptErr bool
name string
exceptErr bool
req types.MsgCancelUnbondingDelegation
expErrMsg string
}{
{
Name: "invalid height",
ExceptErr: true,
name: "entry not found at height",
exceptErr: true,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: resUnbond.ValidatorAddress,
Amount: sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(4)),
CreationHeight: 0,
CreationHeight: 11,
},
expErrMsg: "unbonding delegation entry is not found at block height",
},
{
Name: "invalid coin",
ExceptErr: true,
name: "invalid height",
exceptErr: true,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: resUnbond.ValidatorAddress,
Amount: sdk.NewCoin("dump_coin", sdk.NewInt(4)),
Amount: sdk.NewCoin(stakingKeeper.BondDenom(ctx), sdk.NewInt(4)),
CreationHeight: 0,
},
expErrMsg: "invalid height",
},
{
name: "invalid coin",
exceptErr: true,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: resUnbond.ValidatorAddress,
Amount: sdk.NewCoin("dump_coin", sdk.NewInt(4)),
CreationHeight: 10,
},
expErrMsg: "invalid coin denomination",
},
{
Name: "validator not exists",
ExceptErr: true,
name: "validator not exists",
exceptErr: true,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: sdk.ValAddress(sdk.AccAddress("asdsad")).String(),
Amount: unbondingAmount,
CreationHeight: 0,
CreationHeight: 10,
},
expErrMsg: "validator does not exist",
},
{
Name: "invalid delegator address",
ExceptErr: true,
name: "invalid delegator address",
exceptErr: true,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: "invalid_delegator_addrtess",
ValidatorAddress: resUnbond.ValidatorAddress,
Expand All @@ -126,8 +137,8 @@ func TestCancelUnbondingDelegation(t *testing.T) {
expErrMsg: "decoding bech32 failed",
},
{
Name: "invalid amount",
ExceptErr: true,
name: "invalid amount",
exceptErr: true,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: resUnbond.ValidatorAddress,
Expand All @@ -137,8 +148,8 @@ func TestCancelUnbondingDelegation(t *testing.T) {
expErrMsg: "amount is greater than the unbonding delegation entry balance",
},
{
Name: "success",
ExceptErr: false,
name: "success",
exceptErr: false,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: resUnbond.ValidatorAddress,
Expand All @@ -147,8 +158,8 @@ func TestCancelUnbondingDelegation(t *testing.T) {
},
},
{
Name: "success",
ExceptErr: false,
name: "success",
exceptErr: false,
req: types.MsgCancelUnbondingDelegation{
DelegatorAddress: resUnbond.DelegatorAddress,
ValidatorAddress: resUnbond.ValidatorAddress,
Expand All @@ -159,9 +170,9 @@ func TestCancelUnbondingDelegation(t *testing.T) {
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
_, err := msgServer.CancelUnbondingDelegation(ctx, &testCase.req)
if testCase.ExceptErr {
if testCase.exceptErr {
assert.ErrorContains(t, err, testCase.expErrMsg)
} else {
assert.NilError(t, err)
Expand Down
7 changes: 2 additions & 5 deletions testutil/testnet/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

cmttypes "github.com/cometbft/cometbft/types"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -110,12 +111,8 @@ func (b *GenesisBuilder) GenTx(privVal secp256k1.PrivKey, val cmttypes.GenesisVa
if err != nil {
panic(err)
}
_, err = sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
panic(err)
}

if err := msg.ValidateBasic(); err != nil {
if err := msg.Validate(); err != nil {
panic(err)
}

Expand Down
3 changes: 2 additions & 1 deletion x/distribution/testutil/staking_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"cosmossdk.io/math"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
Expand All @@ -12,7 +13,7 @@ import (

func CreateValidator(pk cryptotypes.PubKey, stake math.Int) (stakingtypes.Validator, error) {
valConsAddr := sdk.GetConsAddress(pk)
val, err := stakingtypes.NewValidator(sdk.ValAddress(valConsAddr), pk, stakingtypes.Description{})
val, err := stakingtypes.NewValidator(sdk.ValAddress(valConsAddr), pk, stakingtypes.Description{Moniker: "TestValidator"})
val.Tokens = stake
val.DelegatorShares = math.LegacyNewDecFromInt(val.Tokens)
return val, err
Expand Down
2 changes: 1 addition & 1 deletion x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl
if err != nil {
return txf, nil, err
}
if err := msg.ValidateBasic(); err != nil {
if err := msg.Validate(); err != nil {
return txf, nil, err
}

Expand Down
4 changes: 4 additions & 0 deletions x/staking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"errors"
"testing"

"cosmossdk.io/math"
Expand Down Expand Up @@ -52,6 +53,9 @@ func (s *KeeperTestSuite) SetupTest() {
accountKeeper.EXPECT().GetModuleAddress(stakingtypes.NotBondedPoolName).Return(notBondedAcc.GetAddress())
accountKeeper.EXPECT().StringToBytes(authtypes.NewModuleAddress(govtypes.ModuleName).String()).Return(authtypes.NewModuleAddress(govtypes.ModuleName), nil).AnyTimes()
accountKeeper.EXPECT().BytesToString(authtypes.NewModuleAddress(govtypes.ModuleName)).Return(authtypes.NewModuleAddress(govtypes.ModuleName).String(), nil).AnyTimes()
accountKeeper.EXPECT().StringToBytes("").Return(nil, errors.New("empty address string is not allowed")).AnyTimes()
accountKeeper.EXPECT().StringToBytes("invalid").Return(nil, errors.New("invalid bech32 string")).AnyTimes()

bankKeeper := stakingtestutil.NewMockBankKeeper(ctrl)

keeper := stakingkeeper.NewKeeper(
Expand Down
Loading

0 comments on commit 7132a76

Please sign in to comment.