refactor(x/slashing)!: remove Accounts String (#20026)

This commit is contained in:
Julián Toledano 2024-04-12 16:14:47 +02:00 committed by GitHub
parent 62d2da5d5c
commit ff8f7229ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 38 additions and 14 deletions

View File

@ -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`

View File

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

View File

@ -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{

View File

@ -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++ {

View File

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

View File

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

View File

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