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(x/slashing)!: remove Accounts String #20026

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions x/slashing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
* `Migrate` now takes a `ValidatorAddressCodec` as argument.
* `Migrator` has a new field of `ValidatorAddressCodec` type.
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
* [#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`
Expand Down
13 changes: 9 additions & 4 deletions x/slashing/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package keeper
import (
"context"

"cosmossdk.io/core/address"
v4 "cosmossdk.io/x/slashing/migrations/v4"

"github.com/cosmos/cosmos-sdk/runtime"
)

// 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.
Expand All @@ -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)
}
8 changes: 6 additions & 2 deletions x/slashing/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v4

import (
"context"
"cosmossdk.io/core/address"

"github.com/bits-and-blooms/bitset"
gogotypes "github.com/cosmos/gogoproto/types"
Expand All @@ -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{
Expand Down
8 changes: 6 additions & 2 deletions x/slashing/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"cosmossdk.io/x/slashing"
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"
Expand All @@ -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++ {
Expand All @@ -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++ {
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions x/slashing/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
9 changes: 6 additions & 3 deletions x/slashing/simulation/proposals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

Expand All @@ -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)
Expand Down
Loading