From 266edc0a93bef094220e26c61261948ec577cc13 Mon Sep 17 00:00:00 2001 From: Marko Date: Sat, 12 Aug 2023 17:45:41 +0200 Subject: [PATCH] refactor: remove staking infraction type dep from evidence and slashing (#17335) Co-authored-by: marbar3778 --- CHANGELOG.md | 1 + x/evidence/go.mod | 4 +++- x/evidence/go.sum | 6 ++---- x/evidence/keeper/infraction.go | 4 ++-- x/evidence/testutil/expected_keepers_mocks.go | 3 ++- x/evidence/types/expected_keepers.go | 3 ++- x/slashing/keeper/infractions.go | 4 ++-- x/slashing/keeper/keeper.go | 10 +++++----- x/slashing/keeper/keeper_test.go | 8 ++++---- x/slashing/testutil/expected_keepers_mocks.go | 3 ++- x/slashing/types/expected_keepers.go | 3 ++- x/staking/keeper/slash.go | 3 ++- x/staking/testutil/expected_keepers_mocks.go | 3 ++- x/staking/types/expected_keepers.go | 3 ++- 14 files changed, 33 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 846de58158..5aad2c4790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/genutil) [#17098](https://github.com/cosmos/cosmos-sdk/pull/17098) `GenAppStateFromConfig`, AddGenesisAccountCmd and `GenTxCmd` takes an addresscodec to decode addresses * (x/distribution) [#17098](https://github.com/cosmos/cosmos-sdk/pull/17098) `NewMsgDepositValidatorRewardsPool`, `NewMsgFundCommunityPool`, `NewMsgWithdrawValidatorCommission` and `NewMsgWithdrawDelegatorReward` takes a string instead of `sdk.ValAddress` or `sdk.AccAddress` * (client) [#17215](https://github.com/cosmos/cosmos-sdk/pull/17215) `server.StartCmd`,`server.ExportCmd`,`server.NewRollbackCmd`,`pruning.Cmd`,`genutilcli.InitCmd`,`genutilcli.GenTxCmd`,`genutilcli.CollectGenTxsCmd`,`genutilcli.AddGenesisAccountCmd`, do not take a home directory anymore. It is inferred from the root command. +* (x/staking) [#17335](https://github.com/cosmos/cosmos-sdk/pull/17335) Remove usage of `"github.com/cosmos/cosmos-sdk/x/staking/types".Infraction_*` in favour of `"cosmossdk.io/api/cosmos/staking/v1beta1".Infraction_` in order to remove dependency between modules on staking ### CLI Breaking Changes diff --git a/x/evidence/go.mod b/x/evidence/go.mod index ca7da3fe33..c48aa27d62 100644 --- a/x/evidence/go.mod +++ b/x/evidence/go.mod @@ -10,7 +10,7 @@ require ( cosmossdk.io/errors v1.0.0 cosmossdk.io/log v1.2.0 cosmossdk.io/math v1.0.1 - cosmossdk.io/store v1.0.0-alpha.1 + cosmossdk.io/store v1.0.0-alpha.1.0.20230728080422-54ed7dab3982 github.com/cometbft/cometbft v0.38.0-rc3 github.com/cosmos/cosmos-proto v1.0.0-beta.3 github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230713190037-6a0ab4fd167f @@ -156,3 +156,5 @@ require ( // Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities. // TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409 replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.1 + +replace github.com/cosmos/cosmos-sdk => ../../. diff --git a/x/evidence/go.sum b/x/evidence/go.sum index 7744882a96..2d3c09ebb0 100644 --- a/x/evidence/go.sum +++ b/x/evidence/go.sum @@ -49,8 +49,8 @@ cosmossdk.io/log v1.2.0 h1:BbykkDsutXPSy8RojFB3KZEWyvMsToLy0ykb/ZhsLqQ= cosmossdk.io/log v1.2.0/go.mod h1:GNSCc/6+DhFIj1aLn/j7Id7PaO8DzNylUZoOYBL9+I4= cosmossdk.io/math v1.0.1 h1:Qx3ifyOPaMLNH/89WeZFH268yCvU4xEcnPLu3sJqPPg= cosmossdk.io/math v1.0.1/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k= -cosmossdk.io/store v1.0.0-alpha.1 h1:/151XxAgm0tiKuYrtJzMG61lf6enpPuP+D/hIN8cRjQ= -cosmossdk.io/store v1.0.0-alpha.1/go.mod h1:ejgU9GhRGMNBduVnDwC3RyhOmu4uKlNQlTiJgPmbDkI= +cosmossdk.io/store v1.0.0-alpha.1.0.20230728080422-54ed7dab3982 h1:61YFeW2AhwwPfoJWzNJWvVubCj32sm5jZkJfraS9pDQ= +cosmossdk.io/store v1.0.0-alpha.1.0.20230728080422-54ed7dab3982/go.mod h1:QAF9zeRa/9ghuv7E8NS9SzWqRbgVNwH/dZwGhYDHUjI= cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI= cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= @@ -173,8 +173,6 @@ github.com/cosmos/cosmos-db v1.0.0 h1:EVcQZ+qYag7W6uorBKFPvX6gRjw6Uq2hIh4hCWjuQ0 github.com/cosmos/cosmos-db v1.0.0/go.mod h1:iBvi1TtqaedwLdcrZVYRSSCb6eSy61NLj4UNmdIgs0U= github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o= github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I= -github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230713190037-6a0ab4fd167f h1:Gbk5m5fb7/W013mOFWrXJ4XVqModmrZAJ/9hhdMDNs4= -github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230713190037-6a0ab4fd167f/go.mod h1:zhOSfCxf4bY9XMqZDthyyCu1DrDYbgn9oZjxTkaVAbM= github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY= github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw= github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE= diff --git a/x/evidence/keeper/infraction.go b/x/evidence/keeper/infraction.go index e9655ccbd8..799a28ac7a 100644 --- a/x/evidence/keeper/infraction.go +++ b/x/evidence/keeper/infraction.go @@ -4,10 +4,10 @@ import ( "context" "fmt" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/x/evidence/types" sdk "github.com/cosmos/cosmos-sdk/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // HandleEquivocationEvidence implements an equivocation evidence handler. Assuming the @@ -123,7 +123,7 @@ func (k Keeper) handleEquivocationEvidence(ctx context.Context, evidence *types. consAddr, slashFractionDoubleSign, evidence.GetValidatorPower(), distributionHeight, - stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, + st.Infraction_INFRACTION_DOUBLE_SIGN, ) if err != nil { return err diff --git a/x/evidence/testutil/expected_keepers_mocks.go b/x/evidence/testutil/expected_keepers_mocks.go index 400a48a694..b74a743cba 100644 --- a/x/evidence/testutil/expected_keepers_mocks.go +++ b/x/evidence/testutil/expected_keepers_mocks.go @@ -9,6 +9,7 @@ import ( reflect "reflect" time "time" + stakingv1beta1 "cosmossdk.io/api/cosmos/staking/v1beta1" comet "cosmossdk.io/core/comet" math "cosmossdk.io/math" types "github.com/cosmos/cosmos-sdk/crypto/types" @@ -194,7 +195,7 @@ func (mr *MockSlashingKeeperMockRecorder) SlashFractionDoubleSign(arg0 interface } // SlashWithInfractionReason mocks base method. -func (m *MockSlashingKeeper) SlashWithInfractionReason(arg0 context.Context, arg1 types0.ConsAddress, arg2 math.LegacyDec, arg3, arg4 int64, arg5 types1.Infraction) error { +func (m *MockSlashingKeeper) SlashWithInfractionReason(arg0 context.Context, arg1 types0.ConsAddress, arg2 math.LegacyDec, arg3, arg4 int64, arg5 stakingv1beta1.Infraction) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SlashWithInfractionReason", arg0, arg1, arg2, arg3, arg4, arg5) ret0, _ := ret[0].(error) diff --git a/x/evidence/types/expected_keepers.go b/x/evidence/types/expected_keepers.go index e29b2c44b2..32a4c05cf1 100644 --- a/x/evidence/types/expected_keepers.go +++ b/x/evidence/types/expected_keepers.go @@ -4,6 +4,7 @@ import ( "context" "time" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/core/comet" "cosmossdk.io/math" @@ -28,7 +29,7 @@ type ( HasValidatorSigningInfo(context.Context, sdk.ConsAddress) bool Tombstone(context.Context, sdk.ConsAddress) error Slash(context.Context, sdk.ConsAddress, math.LegacyDec, int64, int64) error - SlashWithInfractionReason(context.Context, sdk.ConsAddress, math.LegacyDec, int64, int64, stakingtypes.Infraction) error + SlashWithInfractionReason(context.Context, sdk.ConsAddress, math.LegacyDec, int64, int64, st.Infraction) error SlashFractionDoubleSign(context.Context) (math.LegacyDec, error) Jail(context.Context, sdk.ConsAddress) error JailUntil(context.Context, sdk.ConsAddress, time.Time) error diff --git a/x/slashing/keeper/infractions.go b/x/slashing/keeper/infractions.go index d207e7b65f..56e6c7ea23 100644 --- a/x/slashing/keeper/infractions.go +++ b/x/slashing/keeper/infractions.go @@ -6,12 +6,12 @@ import ( "github.com/cockroachdb/errors" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/core/comet" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // HandleValidatorSignature handles a validator signature, must be called once per validator per block. @@ -129,7 +129,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A return err } - coinsBurned, err := k.sk.SlashWithInfractionReason(ctx, consAddr, distributionHeight, power, slashFractionDowntime, stakingtypes.Infraction_INFRACTION_DOWNTIME) + coinsBurned, err := k.sk.SlashWithInfractionReason(ctx, consAddr, distributionHeight, power, slashFractionDowntime, st.Infraction_INFRACTION_DOWNTIME) if err != nil { return err } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index f963d61c38..55e1abee30 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/collections" storetypes "cosmossdk.io/core/store" "cosmossdk.io/log" @@ -13,7 +14,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // Keeper of the slashing store @@ -85,12 +85,12 @@ func (k Keeper) GetPubkey(ctx context.Context, a cryptotypes.Address) (cryptotyp // Slash attempts to slash a validator. The slash is delegated to the staking // module to make the necessary validator changes. It specifies no intraction reason. func (k Keeper) Slash(ctx context.Context, consAddr sdk.ConsAddress, fraction sdkmath.LegacyDec, power, distributionHeight int64) error { - return k.SlashWithInfractionReason(ctx, consAddr, fraction, power, distributionHeight, stakingtypes.Infraction_INFRACTION_UNSPECIFIED) + return k.SlashWithInfractionReason(ctx, consAddr, fraction, power, distributionHeight, st.Infraction_INFRACTION_UNSPECIFIED) } // SlashWithInfractionReason attempts to slash a validator. The slash is delegated to the staking // module to make the necessary validator changes. It specifies an intraction reason. -func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.ConsAddress, fraction sdkmath.LegacyDec, power, distributionHeight int64, infraction stakingtypes.Infraction) error { +func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.ConsAddress, fraction sdkmath.LegacyDec, power, distributionHeight int64, infraction st.Infraction) error { coinsBurned, err := k.sk.SlashWithInfractionReason(ctx, consAddr, distributionHeight, power, fraction, infraction) if err != nil { return err @@ -98,9 +98,9 @@ func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.Cons reasonAttr := sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueUnspecified) switch infraction { - case stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN: + case st.Infraction_INFRACTION_DOUBLE_SIGN: reasonAttr = sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueDoubleSign) - case stakingtypes.Infraction_INFRACTION_DOWNTIME: + case st.Infraction_INFRACTION_DOWNTIME: reasonAttr = sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueMissingSignature) } diff --git a/x/slashing/keeper/keeper_test.go b/x/slashing/keeper/keeper_test.go index e6ea23a78f..26fd56d39a 100644 --- a/x/slashing/keeper/keeper_test.go +++ b/x/slashing/keeper/keeper_test.go @@ -8,6 +8,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" + st "cosmossdk.io/api/cosmos/staking/v1beta1" sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -23,7 +24,6 @@ import ( 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_______________"))) @@ -90,7 +90,7 @@ func (s *KeeperTestSuite) TestJailAndSlash() { s.ctx.BlockHeight(), sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction), slashFractionDoubleSign, - stakingtypes.Infraction_INFRACTION_UNSPECIFIED, + st.Infraction_INFRACTION_UNSPECIFIED, ).Return(sdkmath.NewInt(0), nil) err = s.slashingKeeper.Slash( @@ -114,7 +114,7 @@ func (s *KeeperTestSuite) TestJailAndSlashWithInfractionReason() { s.ctx.BlockHeight(), sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction), slashFractionDoubleSign, - stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, + st.Infraction_INFRACTION_DOUBLE_SIGN, ).Return(sdkmath.NewInt(0), nil) err = s.slashingKeeper.SlashWithInfractionReason( @@ -123,7 +123,7 @@ func (s *KeeperTestSuite) TestJailAndSlashWithInfractionReason() { slashFractionDoubleSign, sdk.TokensToConsensusPower(sdkmath.NewInt(1), sdk.DefaultPowerReduction), s.ctx.BlockHeight(), - stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, + st.Infraction_INFRACTION_DOUBLE_SIGN, ) s.Require().NoError(err) s.stakingKeeper.EXPECT().Jail(s.ctx, consAddr).Return(nil) diff --git a/x/slashing/testutil/expected_keepers_mocks.go b/x/slashing/testutil/expected_keepers_mocks.go index 7772d082d1..adb579ac2f 100644 --- a/x/slashing/testutil/expected_keepers_mocks.go +++ b/x/slashing/testutil/expected_keepers_mocks.go @@ -8,6 +8,7 @@ import ( context "context" reflect "reflect" + stakingv1beta1 "cosmossdk.io/api/cosmos/staking/v1beta1" address "cosmossdk.io/core/address" math "cosmossdk.io/math" types "github.com/cosmos/cosmos-sdk/types" @@ -358,7 +359,7 @@ func (mr *MockStakingKeeperMockRecorder) Slash(arg0, arg1, arg2, arg3, arg4 inte } // SlashWithInfractionReason mocks base method. -func (m *MockStakingKeeper) SlashWithInfractionReason(arg0 context.Context, arg1 types.ConsAddress, arg2, arg3 int64, arg4 math.LegacyDec, arg5 types1.Infraction) (math.Int, error) { +func (m *MockStakingKeeper) SlashWithInfractionReason(arg0 context.Context, arg1 types.ConsAddress, arg2, arg3 int64, arg4 math.LegacyDec, arg5 stakingv1beta1.Infraction) (math.Int, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SlashWithInfractionReason", arg0, arg1, arg2, arg3, arg4, arg5) ret0, _ := ret[0].(math.Int) diff --git a/x/slashing/types/expected_keepers.go b/x/slashing/types/expected_keepers.go index 1cb3c1c1f9..aa1d419995 100644 --- a/x/slashing/types/expected_keepers.go +++ b/x/slashing/types/expected_keepers.go @@ -3,6 +3,7 @@ package types import ( context "context" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/core/address" "cosmossdk.io/math" @@ -46,7 +47,7 @@ type StakingKeeper interface { // slash the validator and delegators of the validator, specifying offense height, offense power, and slash fraction Slash(context.Context, sdk.ConsAddress, int64, int64, math.LegacyDec) (math.Int, error) - SlashWithInfractionReason(context.Context, sdk.ConsAddress, int64, int64, math.LegacyDec, stakingtypes.Infraction) (math.Int, error) + SlashWithInfractionReason(context.Context, sdk.ConsAddress, int64, int64, math.LegacyDec, st.Infraction) (math.Int, error) Jail(context.Context, sdk.ConsAddress) error // jail a validator Unjail(context.Context, sdk.ConsAddress) error // unjail a validator diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 06332a306d..30b24b08c7 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/collections" "cosmossdk.io/math" @@ -190,7 +191,7 @@ func (k Keeper) Slash(ctx context.Context, consAddr sdk.ConsAddress, infractionH } // SlashWithInfractionReason implementation doesn't require the infraction (types.Infraction) to work but is required by Interchain Security. -func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.ConsAddress, infractionHeight, power int64, slashFactor math.LegacyDec, _ types.Infraction) (math.Int, error) { +func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.ConsAddress, infractionHeight, power int64, slashFactor math.LegacyDec, _ st.Infraction) (math.Int, error) { return k.Slash(ctx, consAddr, infractionHeight, power, slashFactor) } diff --git a/x/staking/testutil/expected_keepers_mocks.go b/x/staking/testutil/expected_keepers_mocks.go index da70c5f1ff..463d4d7336 100644 --- a/x/staking/testutil/expected_keepers_mocks.go +++ b/x/staking/testutil/expected_keepers_mocks.go @@ -8,6 +8,7 @@ import ( context "context" reflect "reflect" + stakingv1beta1 "cosmossdk.io/api/cosmos/staking/v1beta1" address "cosmossdk.io/core/address" math "cosmossdk.io/math" crypto "github.com/cometbft/cometbft/proto/tendermint/crypto" @@ -409,7 +410,7 @@ func (mr *MockValidatorSetMockRecorder) Slash(arg0, arg1, arg2, arg3, arg4 inter } // SlashWithInfractionReason mocks base method. -func (m *MockValidatorSet) SlashWithInfractionReason(arg0 context.Context, arg1 types.ConsAddress, arg2, arg3 int64, arg4 math.LegacyDec, arg5 types0.Infraction) (math.Int, error) { +func (m *MockValidatorSet) SlashWithInfractionReason(arg0 context.Context, arg1 types.ConsAddress, arg2, arg3 int64, arg4 math.LegacyDec, arg5 stakingv1beta1.Infraction) (math.Int, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SlashWithInfractionReason", arg0, arg1, arg2, arg3, arg4, arg5) ret0, _ := ret[0].(math.Int) diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index 697cab3594..f46b817ea9 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -5,6 +5,7 @@ import ( cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto" + st "cosmossdk.io/api/cosmos/staking/v1beta1" "cosmossdk.io/core/address" "cosmossdk.io/math" @@ -62,7 +63,7 @@ type ValidatorSet interface { // slash the validator and delegators of the validator, specifying offense height, offense power, and slash fraction Slash(context.Context, sdk.ConsAddress, int64, int64, math.LegacyDec) (math.Int, error) - SlashWithInfractionReason(context.Context, sdk.ConsAddress, int64, int64, math.LegacyDec, Infraction) (math.Int, error) + SlashWithInfractionReason(context.Context, sdk.ConsAddress, int64, int64, math.LegacyDec, st.Infraction) (math.Int, error) Jail(context.Context, sdk.ConsAddress) error // jail a validator Unjail(context.Context, sdk.ConsAddress) error // unjail a validator