refactor(slashing)!: use collections for params state (#16441)

Co-authored-by: marbar3778 <marbar3778@yahoo.com>
This commit is contained in:
samricotta 2023-07-06 10:12:11 +03:00 committed by GitHub
parent fbe087dfbb
commit 5931f1e65e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 42 additions and 56 deletions

View File

@ -65,6 +65,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/distribution) [#16607](https://github.com/cosmos/cosmos-sdk/pull/16607) use collections for `ValidatorHistoricalRewards` state management:
* remove `Keeper`: `IterateValidatorHistoricalRewards`, `GetValidatorHistoricalRewards`, `SetValidatorHistoricalRewards`, `DeleteValidatorHistoricalRewards`, `DeleteValidatorHistoricalReward`, `DeleteAllValidatorHistoricalRewards`
* (x/staking) [#16795](https://github.com/cosmos/cosmos-sdk/pull/16795) `DelegationToDelegationResponse`, `DelegationsToDelegationResponses`, `RedelegationsToRedelegationResponses` are no longer exported.
* [#16441](https://github.com/cosmos/cosmos-sdk/pull/16441) Params state is migrated to collections. `GetParams` has been removed
## [v0.50.0-alpha.1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.0-alpha.1) - 2023-06-30

View File

@ -151,7 +151,7 @@ func initFixture(tb testing.TB) *fixture {
evidencetypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), keeper.NewMsgServerImpl(*evidenceKeeper))
evidencetypes.RegisterQueryServer(integrationApp.QueryHelper(), keeper.NewQuerier(evidenceKeeper))
assert.NilError(tb, slashingKeeper.SetParams(sdkCtx, testutil.TestParams()))
assert.NilError(tb, slashingKeeper.Params.Set(sdkCtx, testutil.TestParams()))
// set default staking params
assert.NilError(tb, stakingKeeper.SetParams(sdkCtx, stakingtypes.DefaultParams()))

View File

@ -117,7 +117,7 @@ func initFixture(tb testing.TB) *fixture {
stakingKeeper.SetParams(sdkCtx, stakingtypes.DefaultParams())
// TestParams set the SignedBlocksWindow to 1000 and MaxMissedBlocksPerWindow to 500
slashingKeeper.SetParams(sdkCtx, testutil.TestParams())
slashingKeeper.Params.Set(sdkCtx, testutil.TestParams())
addrDels := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, sdkCtx, 6, stakingKeeper.TokensFromConsensusPower(sdkCtx, 200))
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)

View File

@ -35,7 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#16008](https://github.com/cosmos/cosmos-sdk/pull/16008) NewKeeper now takes in a KVStoreService instead of KVStoreKey, most functions use context.Context instead of sdk.Context and `IterateEvidence` callback function now returns an error to stop interation (`errors.ErrStopIterating`).
* (keeper) [#15825](https://github.com/cosmos/cosmos-sdk/pull/15825) Evidence constructor now requires an `address.Codec` (`import "cosmossdk.io/core/address"`)
* [#16336](https://github.com/cosmos/cosmos-sdk/pull/16336) Use collections for state management:
* Removed: keeper `SetEvidence`, `GetEvidence`, `IterateEvidences`, `GetAllEvidences`, `MustMarshalEvidence`, `MustUnmarshalEvidence`, `MarshalEvidence`, `UnmarshalEvidence`
* Removed: keeper `SetEvidence`, `GetEvidence`, `IterateEvidences`, `GetAllEvidences`, `MustMarshalEvidence`, `MustUnmarshalEvidence`, `MarshalEvidence`, `UnmarshalEvidence`
### Client Breaking Changes

View File

@ -42,7 +42,7 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, stakingKeeper types.StakingKee
}
}
if err := keeper.SetParams(ctx, data.Params); err != nil {
if err := keeper.Params.Set(ctx, data.Params); err != nil {
panic(err)
}
}
@ -51,7 +51,7 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, stakingKeeper types.StakingKee
// to a genesis file, which can be imported again
// with InitGenesis
func (keeper Keeper) ExportGenesis(ctx sdk.Context) (data *types.GenesisState) {
params, err := keeper.GetParams(ctx)
params, err := keeper.Params.Get(ctx)
if err != nil {
panic(err)
}

View File

@ -14,7 +14,7 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
ctx, keeper := s.ctx, s.slashingKeeper
require := s.Require()
keeper.SetParams(ctx, testutil.TestParams())
keeper.Params.Set(ctx, testutil.TestParams())
consAddr1 := sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
consAddr2 := sdk.ConsAddress(sdk.AccAddress([]byte("addr2_______________")))

View File

@ -25,14 +25,13 @@ func NewQuerier(keeper Keeper) Querier {
}
// Params returns parameters of x/slashing module
func (k Keeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
func (k Querier) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
params, err := k.Keeper.Params.Get(ctx)
if err != nil {
return nil, err
}
params, err := k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, err
return &types.QueryParamsResponse{Params: params}, nil
}
// SigningInfo returns signing-info of a specific validator.

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
@ -25,17 +26,28 @@ type Keeper struct {
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
Schema collections.Schema
Params collections.Item[types.Params]
}
// NewKeeper creates a slashing keeper
func NewKeeper(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, storeService storetypes.KVStoreService, sk types.StakingKeeper, authority string) Keeper {
return Keeper{
sb := collections.NewSchemaBuilder(storeService)
k := Keeper{
storeService: storeService,
cdc: cdc,
legacyAmino: legacyAmino,
sk: sk,
authority: authority,
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
}
schema, err := sb.Build()
if err != nil {
panic(err)
}
k.Schema = schema
return k
}
// GetAuthority returns the x/slashing module's authority.

View File

@ -57,11 +57,11 @@ func (s *KeeperTestSuite) SetupTest() {
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
// set test params
s.slashingKeeper.SetParams(ctx, slashingtestutil.TestParams())
s.slashingKeeper.Params.Set(ctx, slashingtestutil.TestParams())
slashingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
slashingtypes.RegisterQueryServer(queryHelper, s.slashingKeeper)
slashingtypes.RegisterQueryServer(queryHelper, slashingkeeper.NewQuerier(s.slashingKeeper))
s.queryClient = slashingtypes.NewQueryClient(queryHelper)
s.msgServer = slashingkeeper.NewMsgServerImpl(s.slashingKeeper)

View File

@ -39,7 +39,7 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {
// bitmap.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
store := runtime.KVStoreAdapter(m.keeper.storeService.OpenKVStore(ctx))
params, err := m.keeper.GetParams(ctx)
params, err := m.keeper.Params.Get(ctx)
if err != nil {
return err
}

View File

@ -35,7 +35,7 @@ func (k msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParam
}
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetParams(ctx, msg.Params); err != nil {
if err := k.Params.Set(ctx, msg.Params); err != nil {
return nil, err
}

View File

@ -5,19 +5,17 @@ import (
"time"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
// SignedBlocksWindow - sliding window for downtime slashing
func (k Keeper) SignedBlocksWindow(ctx context.Context) (int64, error) {
params, err := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
return params.SignedBlocksWindow, err
}
// MinSignedPerWindow - minimum blocks signed per window
func (k Keeper) MinSignedPerWindow(ctx context.Context) (int64, error) {
params, err := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
if err != nil {
return 0, err
}
@ -32,44 +30,18 @@ func (k Keeper) MinSignedPerWindow(ctx context.Context) (int64, error) {
// DowntimeJailDuration - Downtime unbond duration
func (k Keeper) DowntimeJailDuration(ctx context.Context) (time.Duration, error) {
params, err := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
return params.DowntimeJailDuration, err
}
// SlashFractionDoubleSign - fraction of power slashed in case of double sign
func (k Keeper) SlashFractionDoubleSign(ctx context.Context) (sdkmath.LegacyDec, error) {
params, err := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
return params.SlashFractionDoubleSign, err
}
// SlashFractionDowntime - fraction of power slashed for downtime
func (k Keeper) SlashFractionDowntime(ctx context.Context) (sdkmath.LegacyDec, error) {
params, err := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
return params.SlashFractionDowntime, err
}
// GetParams returns the current x/slashing module parameters.
func (k Keeper) GetParams(ctx context.Context) (params types.Params, err error) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.ParamsKey)
if err != nil {
return params, err
}
if bz == nil {
return params, nil
}
err = k.cdc.Unmarshal(bz, &params)
return params, err
}
// SetParams sets the x/slashing module parameters.
// CONTRACT: This method performs no validation of the parameters.
func (k Keeper) SetParams(ctx context.Context, params types.Params) error {
store := k.storeService.OpenKVStore(ctx)
bz, err := k.cdc.Marshal(&params)
if err != nil {
return err
}
return store.Set(types.ParamsKey, bz)
}

View File

@ -60,7 +60,7 @@ func (s *KeeperTestSuite) TestValidatorMissedBlockBitmap_SmallWindow() {
for _, window := range []int64{100, 32_000} {
params := testutil.TestParams()
params.SignedBlocksWindow = window
require.NoError(keeper.SetParams(ctx, params))
require.NoError(keeper.Params.Set(ctx, params))
// validator misses all blocks in the window
var valIdxOffset int64

View File

@ -147,7 +147,7 @@ func (AppModule) Name() string {
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
m := keeper.NewMigrator(am.keeper, am.legacySubspace)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {

View File

@ -3,6 +3,8 @@ package types
import (
"encoding/binary"
"cosmossdk.io/collections"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/kv"
@ -48,10 +50,10 @@ const (
// - 0x03<accAddrLen (1 Byte)><accAddr_Bytes>: cryptotypes.PubKey
var (
ParamsKey = []byte{0x00} // Prefix for params key
ValidatorSigningInfoKeyPrefix = []byte{0x01} // Prefix for signing info
ValidatorMissedBlockBitmapKeyPrefix = []byte{0x02} // Prefix for missed block bitmap
AddrPubkeyRelationKeyPrefix = []byte{0x03} // Prefix for address-pubkey relation
ParamsKey = collections.NewPrefix(0) // Prefix for params key
ValidatorSigningInfoKeyPrefix = []byte{0x01} // Prefix for signing info
ValidatorMissedBlockBitmapKeyPrefix = []byte{0x02} // Prefix for missed block bitmap
AddrPubkeyRelationKeyPrefix = []byte{0x03} // Prefix for address-pubkey relation
)
// ValidatorSigningInfoKey - stored by *Consensus* address (not operator address)