Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove staking infraction type dep from evidence and slashing #17335

Merged
merged 9 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

### CLI Breaking Changes

Expand Down
3 changes: 1 addition & 2 deletions x/evidence/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"cosmossdk.io/collections"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/x/evidence"
Expand Down Expand Up @@ -96,7 +95,7 @@ func (suite *GenesisTestSuite) TestInitGenesis() {
false,
func() {
_, err := suite.keeper.Evidences.Iterate(suite.ctx, nil)
suite.Require().ErrorIs(err, collections.ErrInvalidIterator)
suite.Require().NoError(err)
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/keeper/infraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion x/evidence/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion x/evidence/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

st "cosmossdk.io/api/cosmos/staking/v1beta1"
"cosmossdk.io/core/comet"
"cosmossdk.io/math"

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions x/slashing/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -85,22 +85,22 @@ 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
}

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)
}

Expand Down
8 changes: 4 additions & 4 deletions x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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_______________")))
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion x/slashing/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion x/slashing/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
context "context"

st "cosmossdk.io/api/cosmos/staking/v1beta1"
"cosmossdk.io/core/address"
"cosmossdk.io/math"

Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

st "cosmossdk.io/api/cosmos/staking/v1beta1"
"cosmossdk.io/collections"
"cosmossdk.io/math"

Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion x/staking/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion x/staking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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

Expand Down