Files
cosmos-sdk/x/slashing/keeper/keeper_test.go
T
atheeshp 7ded32163b chore: x/slashing audit changes (#14211)
## Description

ref: #14158 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-12-09 10:00:44 +00:00

103 lines
3.2 KiB
Go

package keeper_test
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/cosmos/cosmos-sdk/baseapp"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
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"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtestutil "github.com/cosmos/cosmos-sdk/x/slashing/testutil"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
var consAddr = sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
stakingKeeper *slashingtestutil.MockStakingKeeper
slashingKeeper slashingkeeper.Keeper
queryClient slashingtypes.QueryClient
msgServer slashingtypes.MsgServer
}
func (s *KeeperTestSuite) SetupTest() {
key := sdk.NewKVStoreKey(slashingtypes.StoreKey)
testCtx := sdktestutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig()
// gomock initializations
ctrl := gomock.NewController(s.T())
s.stakingKeeper = slashingtestutil.NewMockStakingKeeper(ctrl)
s.ctx = ctx
s.slashingKeeper = slashingkeeper.NewKeeper(
encCfg.Codec,
encCfg.Amino,
key,
s.stakingKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
// set test params
s.slashingKeeper.SetParams(ctx, slashingtestutil.TestParams())
slashingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
slashingtypes.RegisterQueryServer(queryHelper, s.slashingKeeper)
s.queryClient = slashingtypes.NewQueryClient(queryHelper)
s.msgServer = slashingkeeper.NewMsgServerImpl(s.slashingKeeper)
}
func (s *KeeperTestSuite) TestPubkey() {
ctx, keeper := s.ctx, s.slashingKeeper
require := s.Require()
_, pubKey, addr := testdata.KeyTestPubAddr()
require.NoError(keeper.AddPubkey(ctx, pubKey))
expectedPubKey, err := keeper.GetPubkey(ctx, addr.Bytes())
require.NoError(err)
require.Equal(pubKey, expectedPubKey)
}
func (s *KeeperTestSuite) TestJailAndSlash() {
s.stakingKeeper.EXPECT().Slash(s.ctx,
consAddr,
s.ctx.BlockHeight(),
sdk.TokensToConsensusPower(sdk.NewInt(1), sdk.DefaultPowerReduction),
s.slashingKeeper.SlashFractionDoubleSign(s.ctx),
types.Infraction_INFRACTION_DOUBLE_SIGN,
).Return(sdk.NewInt(0))
s.slashingKeeper.Slash(
s.ctx,
consAddr,
s.slashingKeeper.SlashFractionDoubleSign(s.ctx),
sdk.TokensToConsensusPower(sdk.NewInt(1), sdk.DefaultPowerReduction),
s.ctx.BlockHeight(),
types.Infraction_INFRACTION_DOUBLE_SIGN,
)
s.stakingKeeper.EXPECT().Jail(s.ctx, consAddr).Return()
s.slashingKeeper.Jail(s.ctx, consAddr)
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}