Skip to content

Commit

Permalink
use module's store instead of x/param parameter space
Browse files Browse the repository at this point in the history
  • Loading branch information
bermuell committed Nov 13, 2023
1 parent ad40546 commit 849d913
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 157 deletions.
46 changes: 18 additions & 28 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Keeper struct {
storeKey storetypes.StoreKey // TODO: maybe needs to be removed?
storeService store.KVStoreService
cdc codec.BinaryCodec
paramStore paramtypes.Subspace
scopedKeeper ccv.ScopedKeeper
channelKeeper ccv.ChannelKeeper
portKeeper ccv.PortKeeper
Expand Down Expand Up @@ -83,7 +82,6 @@ func NewKeeper(
authority: authority,
storeKey: key,
cdc: cdc,
paramStore: paramSpace,
scopedKeeper: scopedKeeper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
Expand All @@ -108,9 +106,8 @@ func NewKeeper(
// Used only in testing.
func NewNonZeroKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
paramStore: paramSpace,
storeKey: key,
cdc: cdc,
}
}

Expand All @@ -120,45 +117,38 @@ func (k *Keeper) SetStandaloneStakingKeeper(sk ccv.StakingKeeper) {
k.standaloneStakingKeeper = sk
}

// SetParamSpace sets the param space for the consumer keeper.
// Note: this is only used for testing!
func (k *Keeper) SetParamSpace(ctx sdk.Context, ps paramtypes.Subspace) {
k.paramStore = ps
}

// Validates that the consumer keeper is initialized with non-zero and
// non-nil values for all its fields. Otherwise this method will panic.
func (k Keeper) mustValidateFields() {
// Ensures no fields are missed in this validation
// TODO: @MSalopek hangle this better
if reflect.ValueOf(k).NumField() != 20 {
if reflect.ValueOf(k).NumField() != 19 {
panic(fmt.Sprintf("number of fields in consumer keeper is not 19 - have %d", reflect.ValueOf(k).NumField())) // incorrect number
}

if k.validatorAddressCodec == nil || k.consensusAddressCodec == nil {
panic("validator and/or consensus address codec are nil")
}

// Note 17 / 20 fields will be validated,
// Note 16 / 19 fields will be validated,
// hooks are explicitly set after the constructor,
// stakingKeeper is optionally set after the constructor,
ccv.PanicIfZeroOrNil(k.storeKey, "storeKey") // 1
ccv.PanicIfZeroOrNil(k.cdc, "cdc") // 2
ccv.PanicIfZeroOrNil(k.paramStore, "paramStore") // 3
ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 4
ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 5
ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 6
ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 7
ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 8
ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 9
ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 10
ccv.PanicIfZeroOrNil(k.authKeeper, "authKeeper") // 11
ccv.PanicIfZeroOrNil(k.ibcTransferKeeper, "ibcTransferKeeper") // 12
ccv.PanicIfZeroOrNil(k.ibcCoreKeeper, "ibcCoreKeeper") // 13
ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 14
ccv.PanicIfZeroOrNil(k.authority, "authority") // 15
ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 16
ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 17
ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 3
ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 4
ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 5
ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 6
ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 7
ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 8
ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 9
ccv.PanicIfZeroOrNil(k.authKeeper, "authKeeper") // 10
ccv.PanicIfZeroOrNil(k.ibcTransferKeeper, "ibcTransferKeeper") // 11
ccv.PanicIfZeroOrNil(k.ibcCoreKeeper, "ibcCoreKeeper") // 12
ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 13
ccv.PanicIfZeroOrNil(k.authority, "authority") // 14
ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 15
ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 16
}

// Logger returns a module-specific logger.
Expand Down
104 changes: 49 additions & 55 deletions x/ccv/consumer/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@ package keeper

import (
"context"
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
)

// GetParams returns the params for the consumer ccv module
// NOTE: it is different from the GetParams method which is required to implement StakingKeeper interface
func (k Keeper) GetConsumerParams(ctx sdk.Context) ccvtypes.Params {
return ccvtypes.NewParams(
k.GetEnabled(ctx),
k.GetBlocksPerDistributionTransmission(ctx),
k.GetDistributionTransmissionChannel(ctx),
k.GetProviderFeePoolAddrStr(ctx),
k.GetCCVTimeoutPeriod(ctx),
k.GetTransferTimeoutPeriod(ctx),
k.GetConsumerRedistributionFrac(ctx),
k.GetHistoricalEntries(ctx),
k.GetUnbondingPeriod(ctx),
k.GetSoftOptOutThreshold(ctx),
k.GetRewardDenoms(ctx),
k.GetProviderRewardDenoms(ctx),
)
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParametersKey())
var params ccvtypes.Params
err := k.cdc.Unmarshal(bz, &params)
if err != nil {
panic(fmt.Sprintf("error unmarshalling module parameters: %v:", err))
}
return params
}

// SetParams sets the paramset for the consumer module
func (k Keeper) SetParams(ctx sdk.Context, params ccvtypes.Params) {
k.paramStore.SetParamSet(ctx, &params)
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set(types.ParametersKey(), bz)
}

// GetParams implements StakingKeeper GetParams interface method
Expand All @@ -44,98 +42,94 @@ func (k Keeper) GetParams(context.Context) (stakingtypes.Params, error) {

// GetEnabled returns the enabled flag for the consumer module
func (k Keeper) GetEnabled(ctx sdk.Context) bool {
var enabled bool
k.paramStore.Get(ctx, ccvtypes.KeyEnabled, &enabled)
return enabled
params := k.GetConsumerParams(ctx)
return params.Enabled
}

func (k Keeper) GetBlocksPerDistributionTransmission(ctx sdk.Context) int64 {
var bpdt int64
k.paramStore.Get(ctx, ccvtypes.KeyBlocksPerDistributionTransmission, &bpdt)
return bpdt
params := k.GetConsumerParams(ctx)
return params.BlocksPerDistributionTransmission
}

func (k Keeper) SetBlocksPerDistributionTransmission(ctx sdk.Context, bpdt int64) {
k.paramStore.Set(ctx, ccvtypes.KeyBlocksPerDistributionTransmission, bpdt)
params := k.GetConsumerParams(ctx)
params.BlocksPerDistributionTransmission = bpdt
k.SetParams(ctx, params)
}

func (k Keeper) GetDistributionTransmissionChannel(ctx sdk.Context) string {
var s string
k.paramStore.Get(ctx, ccvtypes.KeyDistributionTransmissionChannel, &s)
return s
params := k.GetConsumerParams(ctx)
return params.DistributionTransmissionChannel
}

func (k Keeper) SetDistributionTransmissionChannel(ctx sdk.Context, channel string) {
k.paramStore.Set(ctx, ccvtypes.KeyDistributionTransmissionChannel, channel)
params := k.GetConsumerParams(ctx)
params.DistributionTransmissionChannel = channel
k.SetParams(ctx, params)
}

func (k Keeper) GetProviderFeePoolAddrStr(ctx sdk.Context) string {
var s string
k.paramStore.Get(ctx, ccvtypes.KeyProviderFeePoolAddrStr, &s)
return s
params := k.GetConsumerParams(ctx)
return params.ProviderFeePoolAddrStr
}

func (k Keeper) SetProviderFeePoolAddrStr(ctx sdk.Context, addr string) {
k.paramStore.Set(ctx, ccvtypes.KeyProviderFeePoolAddrStr, addr)
params := k.GetConsumerParams(ctx)
params.ProviderFeePoolAddrStr = addr
k.SetParams(ctx, params)
}

// GetCCVTimeoutPeriod returns the timeout period for sent ccv related ibc packets
func (k Keeper) GetCCVTimeoutPeriod(ctx sdk.Context) time.Duration {
var p time.Duration
k.paramStore.Get(ctx, ccvtypes.KeyCCVTimeoutPeriod, &p)
return p
params := k.GetConsumerParams(ctx)
return params.CcvTimeoutPeriod
}

// GetTransferTimeoutPeriod returns the timeout period for sent transfer related ibc packets
func (k Keeper) GetTransferTimeoutPeriod(ctx sdk.Context) time.Duration {
var p time.Duration
k.paramStore.Get(ctx, ccvtypes.KeyTransferTimeoutPeriod, &p)
return p
params := k.GetConsumerParams(ctx)
return params.TransferTimeoutPeriod
}

// GetConsumerRedistributionFrac returns the fraction of tokens allocated to the consumer redistribution
// address during distribution events. The fraction is a string representing a
// decimal number. For example "0.75" would represent 75%.
func (k Keeper) GetConsumerRedistributionFrac(ctx sdk.Context) string {
var str string
k.paramStore.Get(ctx, ccvtypes.KeyConsumerRedistributionFrac, &str)
return str
params := k.GetConsumerParams(ctx)
return params.ConsumerRedistributionFraction
}

// GetHistoricalEntries returns the number of historical info entries to persist in store
func (k Keeper) GetHistoricalEntries(ctx sdk.Context) int64 {
var n int64
k.paramStore.Get(ctx, ccvtypes.KeyHistoricalEntries, &n)
return n
params := k.GetConsumerParams(ctx)
return params.HistoricalEntries
}

// Only used to set an unbonding period in diff tests
func (k Keeper) SetUnbondingPeriod(ctx sdk.Context, period time.Duration) {
k.paramStore.Set(ctx, ccvtypes.KeyConsumerUnbondingPeriod, period)
params := k.GetConsumerParams(ctx)
params.UnbondingPeriod = period
k.SetParams(ctx, params)
}

func (k Keeper) GetUnbondingPeriod(ctx sdk.Context) time.Duration {
var period time.Duration
k.paramStore.Get(ctx, ccvtypes.KeyConsumerUnbondingPeriod, &period)
return period
params := k.GetConsumerParams(ctx)
return params.UnbondingPeriod
}

// GetSoftOptOutThreshold returns the percentage of validators at the bottom of the set
// that can opt out of running the consumer chain
func (k Keeper) GetSoftOptOutThreshold(ctx sdk.Context) string {
var str string
k.paramStore.Get(ctx, ccvtypes.KeySoftOptOutThreshold, &str)
return str
params := k.GetConsumerParams(ctx)
return params.SoftOptOutThreshold
}

func (k Keeper) GetRewardDenoms(ctx sdk.Context) []string {
var denoms []string
k.paramStore.Get(ctx, ccvtypes.KeyRewardDenoms, &denoms)
return denoms
params := k.GetConsumerParams(ctx)
return params.RewardDenoms
}

func (k Keeper) GetProviderRewardDenoms(ctx sdk.Context) []string {
var denoms []string
k.paramStore.Get(ctx, ccvtypes.KeyProviderRewardDenoms, &denoms)
return denoms
params := k.GetConsumerParams(ctx)
return params.ProviderRewardDenoms
}
5 changes: 1 addition & 4 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ func (k Keeper) QueryParams(goCtx context.Context, req *types.QueryParamsRequest
}

ctx := sdk.UnwrapSDKContext(goCtx)
params, err := k.GetParams(ctx)
if err != nil {
return nil, status.Error(codes.NotFound, "no parameters found")
}
params := k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}

Expand Down
43 changes: 17 additions & 26 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type Keeper struct {
storeService store.KVStoreService

cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
scopedKeeper ccv.ScopedKeeper
channelKeeper ccv.ChannelKeeper
portKeeper ccv.PortKeeper
Expand Down Expand Up @@ -79,7 +78,6 @@ func NewKeeper(
cdc: cdc,
storeKey: key,
authority: authority,
paramSpace: paramSpace,
scopedKeeper: scopedKeeper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
Expand Down Expand Up @@ -114,19 +112,13 @@ func (k Keeper) ConsensusAddressCodec() addresscodec.Codec {
return k.consensusAddressCodec
}

// SetParamSpace sets the param space for the provider keeper.
// Note: this is only used for testing!
func (k *Keeper) SetParamSpace(ctx sdk.Context, ps paramtypes.Subspace) {
k.paramSpace = ps
}

// TODO: @MSalopek -> redo validation; some fields will be removed
// Validates that the provider keeper is initialized with non-zero and
// non-nil values for all its fields. Otherwise this method will panic.
func (k Keeper) mustValidateFields() {
// Ensures no fields are missed in this validation
if reflect.ValueOf(k).NumField() != 18 {
panic("number of fields in provider keeper is not 18")
if reflect.ValueOf(k).NumField() != 17 {
panic("number of fields in provider keeper is not 17")
}

// TODO: @MSalopek -> validate once connected and AccountKeeper interface is updated
Expand All @@ -141,23 +133,22 @@ func (k Keeper) mustValidateFields() {

ccv.PanicIfZeroOrNil(k.cdc, "cdc") // 1
ccv.PanicIfZeroOrNil(k.storeKey, "storeKey") // 2
ccv.PanicIfZeroOrNil(k.paramSpace, "paramSpace") // 3
ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 4
ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 5
ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 6
ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 7
ccv.PanicIfZeroOrNil(k.accountKeeper, "accountKeeper") // 8
ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 9
ccv.PanicIfZeroOrNil(k.stakingKeeper, "stakingKeeper") // 10
ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 11
ccv.PanicIfZeroOrNil(k.distributionKeeper, "distributionKeeper") // 12
ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 13
ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 14
ccv.PanicIfZeroOrNil(k.authority, "authority") // 15
ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 16
ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 17
ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 3
ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 4
ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 5
ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 6
ccv.PanicIfZeroOrNil(k.accountKeeper, "accountKeeper") // 7
ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 8
ccv.PanicIfZeroOrNil(k.stakingKeeper, "stakingKeeper") // 9
ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 10
ccv.PanicIfZeroOrNil(k.distributionKeeper, "distributionKeeper") // 11
ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 12
ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 13
ccv.PanicIfZeroOrNil(k.authority, "authority") // 14
ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 15
ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 16
// TODO: @MSalopek -> validate once connected
// ccv.PanicIfZeroOrNil(k.storeService, "storeService") // 18
// ccv.PanicIfZeroOrNil(k.storeService, "storeService") // 17
}

// Logger returns a module-specific logger.
Expand Down
4 changes: 1 addition & 3 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func (k msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParam
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.Keeper.SetParams(ctx, msg.Params); err != nil {
return nil, err
}
k.Keeper.SetParams(ctx, msg.Params)

return &types.MsgUpdateParamsResponse{}, nil
}
Expand Down
Loading

0 comments on commit 849d913

Please sign in to comment.