diff --git a/x/stakingplus/keeper/keeper_test.go b/x/stakingplus/keeper/keeper_test.go index 81b18e5709..da0abf5945 100644 --- a/x/stakingplus/keeper/keeper_test.go +++ b/x/stakingplus/keeper/keeper_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "fmt" "testing" "github.com/golang/mock/gomock" @@ -9,20 +8,15 @@ import ( "cosmossdk.io/depinject" "cosmossdk.io/log" - "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/runtime" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/Finschia/finschia-sdk/x/stakingplus" "github.com/Finschia/finschia-sdk/x/stakingplus/keeper" "github.com/Finschia/finschia-sdk/x/stakingplus/testutil" ) @@ -31,21 +25,17 @@ type KeeperTestSuite struct { suite.Suite ctx sdk.Context - app *runtime.App - accountKeeper authkeeper.AccountKeeper - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - msgServer stakingtypes.MsgServer - - stranger sdk.AccAddress - grantee sdk.AccAddress - - balance math.Int + app *runtime.App + accountKeeper authkeeper.AccountKeeper + bankKeeper bankkeeper.Keeper + foundationKeeper *testutil.MockFoundationKeeper + stakingKeeper *stakingkeeper.Keeper + msgServer stakingtypes.MsgServer } func (s *KeeperTestSuite) SetupTest() { ctrl := gomock.NewController(s.T()) - foundationKeeper := testutil.NewMockFoundationKeeper(ctrl) + s.foundationKeeper = testutil.NewMockFoundationKeeper(ctrl) app, err := simtestutil.Setup( depinject.Configs( @@ -61,73 +51,9 @@ func (s *KeeperTestSuite) SetupTest() { s.app = app s.ctx = s.app.BaseApp.NewContext(false) - s.msgServer = keeper.NewMsgServerImpl(s.stakingKeeper, foundationKeeper) - - createAddress := func() sdk.AccAddress { - return sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - } - - s.stranger = createAddress() - s.grantee = createAddress() - - s.balance = math.NewInt(1000000) - holders := []sdk.AccAddress{ - s.stranger, - s.grantee, - } - for _, holder := range holders { - amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)) - - // using minttypes here introduces dependency on x/mint - // the work around would be registering a new module account on this suite - // because x/bank already has dependency on x/mint, and we must have dependency - // on x/bank, it's OK to use x/mint here. - minterName := minttypes.ModuleName - err := s.bankKeeper.MintCoins(s.ctx, minterName, amount) - s.Require().NoError(err) - - minter := s.accountKeeper.GetModuleAccount(s.ctx, minterName).GetAddress() - err = s.bankKeeper.SendCoins(s.ctx, minter, holder, amount) - s.Require().NoError(err) - } - - // approve Msg/CreateValidator to grantee - foundationKeeper. - EXPECT(). - Accept(gomock.Any(), s.grantee, NewCreateValidatorAuthorizationMatcher(s.grantee)). - Return(nil) - foundationKeeper. - EXPECT(). - Accept(gomock.Any(), gomock.Any(), gomock.Any()). - Return(sdkerrors.ErrUnauthorized) + s.msgServer = keeper.NewMsgServerImpl(s.stakingKeeper, s.foundationKeeper) } func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } - -type CreateValidatorAuthorizationMatcher struct { - authz stakingplus.CreateValidatorAuthorization -} - -func NewCreateValidatorAuthorizationMatcher(grantee sdk.AccAddress) *CreateValidatorAuthorizationMatcher { - return &CreateValidatorAuthorizationMatcher{ - authz: stakingplus.CreateValidatorAuthorization{ - ValidatorAddress: sdk.ValAddress(grantee).String(), - }, - } -} - -func (c CreateValidatorAuthorizationMatcher) Matches(x interface{}) bool { - msg, ok := x.(sdk.Msg) - if !ok { - return false - } - - resp, err := c.authz.Accept(sdk.Context{}, msg) - return resp.Accept && (err == nil) -} - -func (c CreateValidatorAuthorizationMatcher) String() string { - return fmt.Sprintf("grants %s to %s", c.authz.MsgTypeURL(), c.authz.ValidatorAddress) -} diff --git a/x/stakingplus/keeper/msg_server_test.go b/x/stakingplus/keeper/msg_server_test.go index cfc7120343..ea2ae34bea 100644 --- a/x/stakingplus/keeper/msg_server_test.go +++ b/x/stakingplus/keeper/msg_server_test.go @@ -1,24 +1,62 @@ package keeper_test import ( + "fmt" + "cosmossdk.io/math" + "github.com/Finschia/finschia-sdk/x/stakingplus" + "github.com/cometbft/cometbft/crypto/secp256k1" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/golang/mock/gomock" ) func (s *KeeperTestSuite) TestMsgCreateValidator() { + stranger := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) + grantee := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) + balance := math.NewInt(1000000) + + for _, holder := range []sdk.AccAddress{stranger, grantee} { + amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, balance)) + + // using minttypes here introduces dependency on x/mint + // the work around would be registering a new module account on this suite + // because x/bank already has dependency on x/mint, and we must have dependency + // on x/bank, it's OK to use x/mint here. + minterName := minttypes.ModuleName + err := s.bankKeeper.MintCoins(s.ctx, minterName, amount) + s.Require().NoError(err) + + minter := s.accountKeeper.GetModuleAccount(s.ctx, minterName).GetAddress() + err = s.bankKeeper.SendCoins(s.ctx, minter, holder, amount) + s.Require().NoError(err) + } + + // approve Msg/CreateValidator to grantee + s.foundationKeeper. + EXPECT(). + Accept(gomock.Any(), grantee, NewCreateValidatorAuthorizationMatcher(grantee)). + Return(nil) + s.foundationKeeper. + EXPECT(). + Accept(gomock.Any(), gomock.Any(), gomock.Any()). + Return(sdkerrors.ErrUnauthorized) + testCases := map[string]struct { delegator sdk.AccAddress valid bool }{ "valid request": { - delegator: s.grantee, + delegator: grantee, valid: true, }, "no grant found": { - delegator: s.stranger, + delegator: stranger, }, } @@ -50,3 +88,31 @@ func (s *KeeperTestSuite) TestMsgCreateValidator() { }) } } + +var _ gomock.Matcher = (*CreateValidatorAuthorizationMatcher)(nil) + +type CreateValidatorAuthorizationMatcher struct { + authz stakingplus.CreateValidatorAuthorization +} + +func NewCreateValidatorAuthorizationMatcher(grantee sdk.AccAddress) *CreateValidatorAuthorizationMatcher { + return &CreateValidatorAuthorizationMatcher{ + authz: stakingplus.CreateValidatorAuthorization{ + ValidatorAddress: sdk.ValAddress(grantee).String(), + }, + } +} + +func (c CreateValidatorAuthorizationMatcher) Matches(x interface{}) bool { + msg, ok := x.(sdk.Msg) + if !ok { + return false + } + + resp, err := c.authz.Accept(sdk.Context{}, msg) + return resp.Accept && (err == nil) +} + +func (c CreateValidatorAuthorizationMatcher) String() string { + return fmt.Sprintf("grants %s to %s", c.authz.MsgTypeURL(), c.authz.ValidatorAddress) +}