Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos gas multiplier params #487

Closed
wants to merge 13 commits into from
6 changes: 6 additions & 0 deletions proto/cosmos/params/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ message FeesParams {
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"];
}

message CosmosGasParams {
uint64 cosmos_gas_multiplier_numerator = 1;
uint64 cosmos_gas_multiplier_denominator = 2;
}

message GenesisState {
FeesParams fees_params = 1 [(gogoproto.nullable) = false];
CosmosGasParams cosmos_gas_params = 2 [(gogoproto.nullable) = false];
}
2 changes: 2 additions & 0 deletions x/auth/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ type FeegrantKeeper interface {
type ParamsKeeper interface {
SetFeesParams(ctx sdk.Context, feesParams paramtypes.FeesParams)
GetFeesParams(ctx sdk.Context) paramtypes.FeesParams
SetCosmosGasParams(ctx sdk.Context, cosmosGasParams paramtypes.CosmosGasParams)
GetCosmosGasParams(ctx sdk.Context) paramtypes.CosmosGasParams
}
24 changes: 24 additions & 0 deletions x/params/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ func (k Keeper) GetFeesParams(ctx sdk.Context) types.FeesParams {
return feesParams
}

func (k Keeper) SetCosmosGasParams(ctx sdk.Context, cosmosGasParams types.CosmosGasParams) {
cosmosGasParams.Validate()
subspace, exist := k.GetSubspace(types.ModuleName)
if !exist {
panic("subspace params should exist")
}
subspace.Set(ctx, types.ParamStoreKeyCosmosGasParams, cosmosGasParams)
}

func (k Keeper) GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams {
subspace, _ := k.GetSubspace(types.ModuleName)

if !subspace.Has(ctx, types.ParamStoreKeyCosmosGasParams) {
defaultParams := *types.DefaultCosmosGasParams()
k.SetCosmosGasParams(ctx, defaultParams)
return defaultParams
}

bz := subspace.GetRaw(ctx, types.ParamStoreKeyCosmosGasParams)
var cosmosGasParams types.CosmosGasParams
json.Unmarshal(bz, &cosmosGasParams)
return cosmosGasParams
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+proposal.ModuleName)
Expand Down
23 changes: 23 additions & 0 deletions x/params/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/types"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
defaultGenesis := types.DefaultGenesis()
m.keeper.SetCosmosGasParams(ctx, defaultGenesis.CosmosGasParams)
return nil
}
22 changes: 22 additions & 0 deletions x/params/keeper/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper_test

import (
"testing"

pk "github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/stretchr/testify/require"
)

func TestMigrate1to2(t *testing.T) {
_, ctx, _, _, keeper := testComponents()
m := pk.NewMigrator(keeper)
err := m.Migrate1to2(ctx)
require.Nil(t, err)
cosmosGasParams := keeper.GetCosmosGasParams(ctx)
// ensure set to defaults
defaultParams := types.DefaultGenesis()
require.Equal(t, defaultParams.CosmosGasParams.CosmosGasMultiplierNumerator, cosmosGasParams.CosmosGasMultiplierNumerator)
require.Equal(t, defaultParams.CosmosGasParams.CosmosGasMultiplierDenominator, cosmosGasParams.CosmosGasMultiplierDenominator)
}
4 changes: 3 additions & 1 deletion x/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
proposal.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper)
_ = cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
}

// ProposalContents returns all the params content functions used to
Expand Down Expand Up @@ -147,4 +149,4 @@ func (am AppModule) ExportGenesis(_ sdk.Context, _ codec.JSONCodec) json.RawMess
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }
13 changes: 12 additions & 1 deletion x/params/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ func DefaultFeesParams() *FeesParams {
}
}

func DefaultCosmosGasParams() *CosmosGasParams {
return &CosmosGasParams{
CosmosGasMultiplierNumerator: 1,
CosmosGasMultiplierDenominator: 1,
}
}

// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
FeesParams: *DefaultFeesParams(),
FeesParams: *DefaultFeesParams(),
CosmosGasParams: *DefaultCosmosGasParams(),
}
}

func (gs GenesisState) Validate() error {
if err := gs.CosmosGasParams.Validate(); err != nil {
return err
}
return gs.FeesParams.Validate()
}
30 changes: 30 additions & 0 deletions x/params/types/params.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package types

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var ParamStoreKeyFeesParams = []byte("FeesParams")
var ParamStoreKeyCosmosGasParams = []byte("CosmosGasParams")

func NewFeesParams(minGasPrices sdk.DecCoins) FeesParams {
return FeesParams{
Expand All @@ -18,6 +20,7 @@ func NewFeesParams(minGasPrices sdk.DecCoins) FeesParams {
func ParamKeyTable() KeyTable {
return NewKeyTable(
NewParamSetPair(ParamStoreKeyFeesParams, &FeesParams{}, validateFeesParams),
NewParamSetPair(ParamStoreKeyCosmosGasParams, &CosmosGasParams{}, validateCosmosGasParams),
)
}

Expand All @@ -41,3 +44,30 @@ func validateFeesParams(i interface{}) error {
}
return nil
}

func NewCosmosGasParams(multiplierNumerator uint64, multiplierDenominator uint64) CosmosGasParams {
return CosmosGasParams{
CosmosGasMultiplierNumerator: multiplierNumerator,
CosmosGasMultiplierDenominator: multiplierDenominator,
}
}

func (cg *CosmosGasParams) Validate() error {
if cg.CosmosGasMultiplierDenominator == 0 {
return errors.New("Cosmos Gas Multiplier Denominator can not be 0")
Kbhat1 marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func validateCosmosGasParams(i interface{}) error {
v, ok := i.(CosmosGasParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if err := v.Validate(); err != nil {
return err
}
return nil
}
Loading
Loading