Skip to content

Commit

Permalink
refactor(slashing)!: use collections for params state (cosmos#16441)
Browse files Browse the repository at this point in the history
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
  • Loading branch information
samricotta and tac0turtle authored Jul 6, 2023
1 parent fbe087d commit 5931f1e
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion x/evidence/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_______________")))
Expand Down
11 changes: 5 additions & 6 deletions x/slashing/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion x/slashing/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
38 changes: 5 additions & 33 deletions x/slashing/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion x/slashing/keeper/signing_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions x/slashing/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5931f1e

Please sign in to comment.