Skip to content

Commit

Permalink
refactor(crisis)!: use collections for state management (#16328)
Browse files Browse the repository at this point in the history
Co-authored-by: unknown unknown <unknown@unknown>
  • Loading branch information
testinginprod and unknown unknown committed May 29, 2023
1 parent d225e42 commit f358214
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 103 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* this finalizes the gov collections migration
* Removed: keeper `InsertActiveProposalsQueue`, `RemoveActiveProposalsQueue`, `InsertInactiveProposalsQueue`, `RemoveInactiveProposalsQueue`, `IterateInactiveProposalsQueue`, `IterateActiveProposalsQueue`, `ActiveProposalsQueueIterator`, `InactiveProposalsQueueIterator`
* Remove: types all the key related functions
* (x/crisis) [#16328](https://github.com/cosmos/cosmos-sdk/pull/16328) Use collections for state management:
* Removed: keeper `GetConstantFee`, `SetConstantFee`

### Client Breaking Changes

Expand Down
4 changes: 2 additions & 2 deletions x/crisis/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

// new crisis genesis
func (k *Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
if err := k.SetConstantFee(ctx, data.ConstantFee); err != nil {
if err := k.ConstantFee.Set(ctx, data.ConstantFee); err != nil {
panic(err)
}
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func (k *Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
constantFee, err := k.GetConstantFee(ctx)
constantFee, err := k.ConstantFee.Get(ctx)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions x/crisis/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (s *GenesisTestSuite) SetupTest() {
func (s *GenesisTestSuite) TestImportExportGenesis() {
// default params
constantFee := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1000))
err := s.keeper.SetConstantFee(s.sdkCtx, constantFee)
err := s.keeper.ConstantFee.Set(s.sdkCtx, constantFee)
s.Require().NoError(err)
genesis := s.keeper.ExportGenesis(s.sdkCtx)

// set constant fee to zero
constantFee = sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(0))
err = s.keeper.SetConstantFee(s.sdkCtx, constantFee)
err = s.keeper.ConstantFee.Set(s.sdkCtx, constantFee)
s.Require().NoError(err)

s.keeper.InitGenesis(s.sdkCtx, genesis)
Expand All @@ -71,7 +71,7 @@ func (s *GenesisTestSuite) TestInitGenesis() {
genesisState.ConstantFee = sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1000))
s.keeper.InitGenesis(s.sdkCtx, genesisState)

constantFee, err := s.keeper.GetConstantFee(s.sdkCtx)
constantFee, err := s.keeper.ConstantFee.Get(s.sdkCtx)
s.Require().NoError(err)
s.Require().Equal(genesisState.ConstantFee, constantFee)
}
15 changes: 14 additions & 1 deletion x/crisis/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/log"

Expand All @@ -31,14 +32,18 @@ type Keeper struct {
feeCollectorName string // name of the FeeCollector ModuleAccount

addressCodec address.Codec

Schema collections.Schema
ConstantFee collections.Item[sdk.Coin]
}

// NewKeeper creates a new Keeper object
func NewKeeper(
cdc codec.BinaryCodec, storeService storetypes.KVStoreService, invCheckPeriod uint,
supplyKeeper types.SupplyKeeper, feeCollectorName, authority string, ac address.Codec,
) *Keeper {
return &Keeper{
sb := collections.NewSchemaBuilder(storeService)
k := &Keeper{
storeService: storeService,
cdc: cdc,
routes: make([]types.InvarRoute, 0),
Expand All @@ -47,7 +52,15 @@ func NewKeeper(
feeCollectorName: feeCollectorName,
authority: authority,
addressCodec: ac,

ConstantFee: collections.NewItem(sb, types.ConstantFeeKey, "constant_fee", codec.CollValue[sdk.Coin](cdc)),
}
schema, err := sb.Build()
if err != nil {
panic(err)
}
k.Schema = schema
return k
}

// GetAuthority returns the x/crisis module's authority.
Expand Down
4 changes: 2 additions & 2 deletions x/crisis/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInva
}

ctx := sdk.UnwrapSDKContext(goCtx)
params, err := k.GetConstantFee(ctx)
params, err := k.ConstantFee.Get(goCtx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (k *Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
if err := k.SetConstantFee(sdkCtx, msg.ConstantFee); err != nil {
if err := k.ConstantFee.Set(sdkCtx, msg.ConstantFee); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion x/crisis/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *KeeperTestSuite) SetupTest() {
func (s *KeeperTestSuite) TestMsgVerifyInvariant() {
// default params
constantFee := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000))
err := s.keeper.SetConstantFee(s.ctx, constantFee)
err := s.keeper.ConstantFee.Set(s.ctx, constantFee)
s.Require().NoError(err)

encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
Expand Down
37 changes: 0 additions & 37 deletions x/crisis/keeper/params.go

This file was deleted.

56 changes: 0 additions & 56 deletions x/crisis/keeper/params_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions x/crisis/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func DefaultGenesisState() *GenesisState {

// ValidateGenesis - validate crisis genesis data
func ValidateGenesis(data *GenesisState) error {
if !data.ConstantFee.IsValid() {
return fmt.Errorf("constant fee is invalid")
}
if !data.ConstantFee.IsPositive() {
return fmt.Errorf("constant fee must be positive: %s", data.ConstantFee)
}
Expand Down
4 changes: 3 additions & 1 deletion x/crisis/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package types

import "cosmossdk.io/collections"

const (
// module name
ModuleName = "crisis"

StoreKey = ModuleName
)

var ConstantFeeKey = []byte{0x01}
var ConstantFeeKey = collections.NewPrefix(1)

0 comments on commit f358214

Please sign in to comment.