cosmos-sdk/x/staking/keeper/keeper_test.go

101 lines
3.3 KiB
Go

package keeper_test
import (
"testing"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var (
bondedAcc = authtypes.NewEmptyModuleAccount(stakingtypes.BondedPoolName)
notBondedAcc = authtypes.NewEmptyModuleAccount(stakingtypes.NotBondedPoolName)
PKs = simtestutil.CreateTestPubKeys(500)
)
type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
stakingKeeper *stakingkeeper.Keeper
bankKeeper *stakingtestutil.MockBankKeeper
accountKeeper *stakingtestutil.MockAccountKeeper
queryClient stakingtypes.QueryClient
msgServer stakingtypes.MsgServer
}
func (s *KeeperTestSuite) SetupTest() {
key := sdk.NewKVStoreKey(stakingtypes.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig()
ctrl := gomock.NewController(s.T())
accountKeeper := stakingtestutil.NewMockAccountKeeper(ctrl)
accountKeeper.EXPECT().GetModuleAddress(stakingtypes.BondedPoolName).Return(bondedAcc.GetAddress())
accountKeeper.EXPECT().GetModuleAddress(stakingtypes.NotBondedPoolName).Return(notBondedAcc.GetAddress())
bankKeeper := stakingtestutil.NewMockBankKeeper(ctrl)
keeper := stakingkeeper.NewKeeper(
encCfg.Codec,
key,
accountKeeper,
bankKeeper,
nil,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
keeper.SetParams(ctx, stakingtypes.DefaultParams())
s.ctx = ctx
s.stakingKeeper = keeper
s.bankKeeper = bankKeeper
s.accountKeeper = accountKeeper
stakingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
stakingtypes.RegisterQueryServer(queryHelper, stakingkeeper.Querier{Keeper: keeper})
s.queryClient = stakingtypes.NewQueryClient(queryHelper)
s.msgServer = stakingkeeper.NewMsgServerImpl(keeper)
}
func (s *KeeperTestSuite) TestParams() {
ctx, keeper := s.ctx, s.stakingKeeper
require := s.Require()
expParams := stakingtypes.DefaultParams()
expParams.MaxValidators = 555
expParams.MaxEntries = 111
keeper.SetParams(ctx, expParams)
resParams := keeper.GetParams(ctx)
require.True(expParams.Equal(resParams))
}
func (s *KeeperTestSuite) TestLastTotalPower() {
ctx, keeper := s.ctx, s.stakingKeeper
require := s.Require()
expTotalPower := math.NewInt(10 ^ 9)
keeper.SetLastTotalPower(ctx, expTotalPower)
resTotalPower := keeper.GetLastTotalPower(ctx)
require.True(expTotalPower.Equal(resTotalPower))
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}