diff --git a/x/slashing/CHANGELOG.md b/x/slashing/CHANGELOG.md index 81a7018057d4..121c64c1b0a5 100644 --- a/x/slashing/CHANGELOG.md +++ b/x/slashing/CHANGELOG.md @@ -35,6 +35,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* [#20026](https://github.com/cosmos/cosmos-sdk/pull/20026) Removal of the Address.String() method and related changes: + * `Migrate` now takes a `ValidatorAddressCodec` as argument. + * `Migrator` has a new field of `ValidatorAddressCodec` type. * [#16441](https://github.com/cosmos/cosmos-sdk/pull/16441) Params state is migrated to collections. `GetParams` has been removed. * [#17023](https://github.com/cosmos/cosmos-sdk/pull/17023) Use collections for `ValidatorSigningInfo`: * remove `Keeper`: `SetValidatorSigningInfo`, `GetValidatorSigningInfo`, `IterateValidatorSigningInfos` diff --git a/x/slashing/keeper/migrations.go b/x/slashing/keeper/migrations.go index 8dde29ff7707..9f00003d315b 100644 --- a/x/slashing/keeper/migrations.go +++ b/x/slashing/keeper/migrations.go @@ -3,6 +3,7 @@ package keeper import ( "context" + "cosmossdk.io/core/address" v4 "cosmossdk.io/x/slashing/migrations/v4" "github.com/cosmos/cosmos-sdk/runtime" @@ -10,12 +11,16 @@ import ( // Migrator is a struct for handling in-place store migrations. type Migrator struct { - keeper Keeper + keeper Keeper + valCodec address.ValidatorAddressCodec } // NewMigrator returns a new Migrator. -func NewMigrator(keeper Keeper) Migrator { - return Migrator{keeper: keeper} +func NewMigrator(keeper Keeper, valCodec address.ValidatorAddressCodec) Migrator { + return Migrator{ + keeper: keeper, + valCodec: valCodec, + } } // Migrate1to2 migrates from version 1 to 2. @@ -40,5 +45,5 @@ func (m Migrator) Migrate3to4(ctx context.Context) error { if err != nil { return err } - return v4.Migrate(ctx, m.keeper.cdc, store, params) + return v4.Migrate(ctx, m.keeper.cdc, store, params, m.valCodec) } diff --git a/x/slashing/migrations/v4/migrate.go b/x/slashing/migrations/v4/migrate.go index f81566996e68..331068c32824 100644 --- a/x/slashing/migrations/v4/migrate.go +++ b/x/slashing/migrations/v4/migrate.go @@ -6,6 +6,7 @@ import ( "github.com/bits-and-blooms/bitset" gogotypes "github.com/cosmos/gogoproto/types" + "cosmossdk.io/core/address" "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/slashing/types" @@ -17,12 +18,15 @@ import ( // Migrate migrates state to consensus version 4. Specifically, the migration // deletes all existing validator bitmap entries and replaces them with a real // "chunked" bitmap. -func Migrate(ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, params types.Params) error { +func Migrate(ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, params types.Params, addressCodec address.ValidatorAddressCodec) error { // Get all the missed blocks for each validator, based on the existing signing // info. var missedBlocks []types.ValidatorMissedBlocks iterateValidatorSigningInfos(ctx, cdc, store, func(addr sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) { - bechAddr := addr.String() + bechAddr, err := addressCodec.BytesToString(addr) + if err != nil { + return true + } localMissedBlocks := GetValidatorMissedBlocks(ctx, cdc, store, addr, params) missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{ diff --git a/x/slashing/migrations/v4/migrate_test.go b/x/slashing/migrations/v4/migrate_test.go index 41d8995af6e4..84fe79544624 100644 --- a/x/slashing/migrations/v4/migrate_test.go +++ b/x/slashing/migrations/v4/migrate_test.go @@ -12,6 +12,7 @@ import ( v4 "cosmossdk.io/x/slashing/migrations/v4" slashingtypes "cosmossdk.io/x/slashing/types" + "github.com/cosmos/cosmos-sdk/codec/address" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" @@ -27,9 +28,12 @@ func TestMigrate(t *testing.T) { ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) params := slashingtypes.Params{SignedBlocksWindow: 100} + valCodec := address.NewBech32Codec("cosmosvalcons") + consStrAddr, err := valCodec.BytesToString(consAddr) + require.NoError(t, err) // store old signing info and bitmap entries - bz := cdc.MustMarshal(&slashingtypes.ValidatorSigningInfo{Address: consAddr.String()}) + bz := cdc.MustMarshal(&slashingtypes.ValidatorSigningInfo{Address: consStrAddr}) store.Set(v4.ValidatorSigningInfoKey(consAddr), bz) for i := int64(0); i < params.SignedBlocksWindow; i++ { @@ -39,7 +43,7 @@ func TestMigrate(t *testing.T) { store.Set(v4.ValidatorMissedBlockBitArrayKey(consAddr, i), bz) } - err := v4.Migrate(ctx, cdc, store, params) + err = v4.Migrate(ctx, cdc, store, params, valCodec) require.NoError(t, err) for i := int64(0); i < params.SignedBlocksWindow; i++ { diff --git a/x/slashing/module.go b/x/slashing/module.go index dab442d3860d..c5ae4fef1919 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -103,7 +103,7 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { // RegisterMigrations registers module migrations. func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { - m := keeper.NewMigrator(am.keeper) + m := keeper.NewMigrator(am.keeper, am.stakingKeeper.ValidatorAddressCodec()) if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) diff --git a/x/slashing/simulation/proposals.go b/x/slashing/simulation/proposals.go index 5d37412ad821..1ea39d97f19a 100644 --- a/x/slashing/simulation/proposals.go +++ b/x/slashing/simulation/proposals.go @@ -33,10 +33,15 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg { } // SimulateMsgUpdateParams returns a random MsgUpdateParams -func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.Codec) (sdk.Msg, error) { +func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, ac coreaddress.Codec) (sdk.Msg, error) { // use the default gov module account address as authority var authority sdk.AccAddress = address.Module("gov") + authorityAddr, err := ac.BytesToString(authority) + if err != nil { + return nil, err + } + params := types.DefaultParams() params.DowntimeJailDuration = time.Duration(simtypes.RandTimestamp(r).UnixNano()) params.SignedBlocksWindow = int64(simtypes.RandIntBetween(r, 1, 1000)) @@ -45,7 +50,7 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.C params.SlashFractionDowntime = sdkmath.LegacyNewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) return &types.MsgUpdateParams{ - Authority: authority.String(), + Authority: authorityAddr, Params: params, }, nil } diff --git a/x/slashing/simulation/proposals_test.go b/x/slashing/simulation/proposals_test.go index 6a67de33fd29..d2b553964a00 100644 --- a/x/slashing/simulation/proposals_test.go +++ b/x/slashing/simulation/proposals_test.go @@ -12,7 +12,6 @@ import ( "cosmossdk.io/x/slashing/types" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) @@ -21,6 +20,7 @@ func TestProposalMsgs(t *testing.T) { // initialize parameters s := rand.NewSource(1) r := rand.New(s) + ac := codectestutil.CodecOptions{}.GetAddressCodec() accounts := simtypes.RandomAccounts(r, 3) @@ -34,12 +34,15 @@ func TestProposalMsgs(t *testing.T) { assert.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey()) assert.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight()) - msg, err := w0.MsgSimulatorFn()(r, accounts, codectestutil.CodecOptions{}.GetAddressCodec()) + msg, err := w0.MsgSimulatorFn()(r, accounts, ac) assert.NilError(t, err) msgUpdateParams, ok := msg.(*types.MsgUpdateParams) assert.Assert(t, ok) - assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority) + moduleAddr, err := ac.BytesToString(address.Module("gov")) + assert.NilError(t, err) + + assert.Equal(t, moduleAddr, msgUpdateParams.Authority) assert.Equal(t, int64(905), msgUpdateParams.Params.SignedBlocksWindow) assert.DeepEqual(t, sdkmath.LegacyNewDecWithPrec(7, 2), msgUpdateParams.Params.MinSignedPerWindow) assert.DeepEqual(t, sdkmath.LegacyNewDecWithPrec(60, 2), msgUpdateParams.Params.SlashFractionDoubleSign)