mukan-sdk/x/slashing/keeper/keeper_test.go
Mukan Erkin Törük 20afb5db80
Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:24 +03:00

136 lines
4.6 KiB
Go

package keeper_test
import (
"testing"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttime "github.com/cometbft/cometbft/types/time"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/runtime"
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"
stakingtypes "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 := storetypes.NewKVStoreKey(slashingtypes.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := sdktestutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig()
// gomock initializations
ctrl := gomock.NewController(s.T())
s.stakingKeeper = slashingtestutil.NewMockStakingKeeper(ctrl)
s.stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes()
s.stakingKeeper.EXPECT().ConsensusAddressCodec().Return(address.NewBech32Codec("cosmosvalcons")).AnyTimes()
s.ctx = ctx
s.slashingKeeper = slashingkeeper.NewKeeper(
encCfg.Codec,
encCfg.Amino,
storeService,
s.stakingKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
// set test params
s.Require().NoError(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() {
slashFractionDoubleSign, err := s.slashingKeeper.SlashFractionDoubleSign(s.ctx)
s.Require().NoError(err)
s.stakingKeeper.EXPECT().SlashWithInfractionReason(s.ctx,
consAddr,
s.ctx.BlockHeight(),
sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
slashFractionDoubleSign,
stakingtypes.Infraction_INFRACTION_UNSPECIFIED,
).Return(sdkmath.NewInt(0), nil)
s.Require().NoError(s.slashingKeeper.Slash(
s.ctx,
consAddr,
slashFractionDoubleSign,
sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
s.ctx.BlockHeight(),
))
s.stakingKeeper.EXPECT().Jail(s.ctx, consAddr).Return(nil)
s.Require().NoError(s.slashingKeeper.Jail(s.ctx, consAddr))
}
func (s *KeeperTestSuite) TestJailAndSlashWithInfractionReason() {
slashFractionDoubleSign, err := s.slashingKeeper.SlashFractionDoubleSign(s.ctx)
s.Require().NoError(err)
s.stakingKeeper.EXPECT().SlashWithInfractionReason(s.ctx,
consAddr,
s.ctx.BlockHeight(),
sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
slashFractionDoubleSign,
stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN,
).Return(sdkmath.NewInt(0), nil)
s.Require().NoError(s.slashingKeeper.SlashWithInfractionReason(
s.ctx,
consAddr,
slashFractionDoubleSign,
sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction),
s.ctx.BlockHeight(),
stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN,
))
s.stakingKeeper.EXPECT().Jail(s.ctx, consAddr).Return(nil)
s.Require().NoError(s.slashingKeeper.Jail(s.ctx, consAddr))
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}