github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/keeper/keeper_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
     7  	cmttime "github.com/cometbft/cometbft/types/time"
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	sdkmath "cosmossdk.io/math"
    12  	storetypes "cosmossdk.io/store/types"
    13  
    14  	"github.com/cosmos/cosmos-sdk/baseapp"
    15  	"github.com/cosmos/cosmos-sdk/codec/address"
    16  	"github.com/cosmos/cosmos-sdk/runtime"
    17  	sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
    18  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    19  	sdk "github.com/cosmos/cosmos-sdk/types"
    20  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    21  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
    22  	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
    23  	slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
    24  	slashingtestutil "github.com/cosmos/cosmos-sdk/x/slashing/testutil"
    25  	slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
    26  	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
    27  )
    28  
    29  var consAddr = sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
    30  
    31  type KeeperTestSuite struct {
    32  	suite.Suite
    33  
    34  	ctx            sdk.Context
    35  	stakingKeeper  *slashingtestutil.MockStakingKeeper
    36  	slashingKeeper slashingkeeper.Keeper
    37  	queryClient    slashingtypes.QueryClient
    38  	msgServer      slashingtypes.MsgServer
    39  }
    40  
    41  func (s *KeeperTestSuite) SetupTest() {
    42  	key := storetypes.NewKVStoreKey(slashingtypes.StoreKey)
    43  	storeService := runtime.NewKVStoreService(key)
    44  	testCtx := sdktestutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
    45  	ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()})
    46  	encCfg := moduletestutil.MakeTestEncodingConfig()
    47  
    48  	// gomock initializations
    49  	ctrl := gomock.NewController(s.T())
    50  	s.stakingKeeper = slashingtestutil.NewMockStakingKeeper(ctrl)
    51  	s.stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes()
    52  	s.stakingKeeper.EXPECT().ConsensusAddressCodec().Return(address.NewBech32Codec("cosmosvalcons")).AnyTimes()
    53  
    54  	s.ctx = ctx
    55  	s.slashingKeeper = slashingkeeper.NewKeeper(
    56  		encCfg.Codec,
    57  		encCfg.Amino,
    58  		storeService,
    59  		s.stakingKeeper,
    60  		authtypes.NewModuleAddress(govtypes.ModuleName).String(),
    61  	)
    62  	// set test params
    63  	s.slashingKeeper.SetParams(ctx, slashingtestutil.TestParams())
    64  
    65  	slashingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
    66  	queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
    67  	slashingtypes.RegisterQueryServer(queryHelper, s.slashingKeeper)
    68  
    69  	s.queryClient = slashingtypes.NewQueryClient(queryHelper)
    70  	s.msgServer = slashingkeeper.NewMsgServerImpl(s.slashingKeeper)
    71  }
    72  
    73  func (s *KeeperTestSuite) TestPubkey() {
    74  	ctx, keeper := s.ctx, s.slashingKeeper
    75  	require := s.Require()
    76  
    77  	_, pubKey, addr := testdata.KeyTestPubAddr()
    78  	require.NoError(keeper.AddPubkey(ctx, pubKey))
    79  
    80  	expectedPubKey, err := keeper.GetPubkey(ctx, addr.Bytes())
    81  	require.NoError(err)
    82  	require.Equal(pubKey, expectedPubKey)
    83  }
    84  
    85  func (s *KeeperTestSuite) TestJailAndSlash() {
    86  	slashFractionDoubleSign, err := s.slashingKeeper.SlashFractionDoubleSign(s.ctx)
    87  	s.Require().NoError(err)
    88  
    89  	s.stakingKeeper.EXPECT().SlashWithInfractionReason(s.ctx,
    90  		consAddr,
    91  		s.ctx.BlockHeight(),
    92  		sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
    93  		slashFractionDoubleSign,
    94  		stakingtypes.Infraction_INFRACTION_UNSPECIFIED,
    95  	).Return(sdkmath.NewInt(0), nil)
    96  
    97  	s.slashingKeeper.Slash(
    98  		s.ctx,
    99  		consAddr,
   100  		slashFractionDoubleSign,
   101  		sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
   102  		s.ctx.BlockHeight(),
   103  	)
   104  
   105  	s.stakingKeeper.EXPECT().Jail(s.ctx, consAddr).Return(nil)
   106  	s.slashingKeeper.Jail(s.ctx, consAddr)
   107  }
   108  
   109  func (s *KeeperTestSuite) TestJailAndSlashWithInfractionReason() {
   110  	slashFractionDoubleSign, err := s.slashingKeeper.SlashFractionDoubleSign(s.ctx)
   111  	s.Require().NoError(err)
   112  
   113  	s.stakingKeeper.EXPECT().SlashWithInfractionReason(s.ctx,
   114  		consAddr,
   115  		s.ctx.BlockHeight(),
   116  		sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
   117  		slashFractionDoubleSign,
   118  		stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN,
   119  	).Return(sdkmath.NewInt(0), nil)
   120  
   121  	s.slashingKeeper.SlashWithInfractionReason(
   122  		s.ctx,
   123  		consAddr,
   124  		slashFractionDoubleSign,
   125  		sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
   126  		s.ctx.BlockHeight(),
   127  		stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN,
   128  	)
   129  
   130  	s.stakingKeeper.EXPECT().Jail(s.ctx, consAddr).Return(nil)
   131  	s.slashingKeeper.Jail(s.ctx, consAddr)
   132  }
   133  
   134  func TestKeeperTestSuite(t *testing.T) {
   135  	suite.Run(t, new(KeeperTestSuite))
   136  }