Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(staking): add missing msgServer tests and move ValidateBasic() logic to msgServer #15820

Merged
merged 24 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
81b76cb
wip: move ValidateBasic() logic tomsg_server
likhita-809 Apr 12, 2023
ff32a6f
fix tests
likhita-809 Apr 13, 2023
1ca1b64
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
f557f5f
wip: updates
likhita-809 Apr 13, 2023
2f3520b
wip: updates
likhita-809 Apr 13, 2023
3e4a05e
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
39ee123
wip: more updates
likhita-809 Apr 13, 2023
6ef8f65
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
6f27769
wip: add more msg server tests
likhita-809 Apr 13, 2023
5792008
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
d9462ca
fix integration tests
likhita-809 Apr 13, 2023
95bd0fd
fix lint
likhita-809 Apr 13, 2023
247eaed
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
2e32a63
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
8311941
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 13, 2023
048dfd5
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 14, 2023
c527d99
wip: address review comments
likhita-809 Apr 14, 2023
eef62e8
address more comments
likhita-809 Apr 14, 2023
699ea2d
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 14, 2023
723a376
address review comments and refactor tests
likhita-809 Apr 14, 2023
f4cd62e
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Apr 14, 2023
ca18381
remove unncessary expected keepers
likhita-809 Apr 14, 2023
7ed4842
fix failing test
likhita-809 Apr 14, 2023
024c2b1
add nit
likhita-809 Apr 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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