Skip to content

Commit

Permalink
fix: validate proposed genesis state in InitGenesis (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtieri authored Sep 19, 2023
1 parent b4ca769 commit 5bb6779
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions middleware/packet-forward-middleware/router/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetSendEnabled retrieves the send enabled boolean from the paramstore
// GetFeePercentage retrieves the fee percentage for forwarded packets from the store.
func (k Keeper) GetFeePercentage(ctx sdk.Context) sdk.Dec {
var res sdk.Dec
k.paramSpace.Get(ctx, types.KeyFeePercentage, &res)
return res
}

// GetParams returns the total set of ibc-transfer parameters.
// GetParams returns the total set of pfm parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetFeePercentage(ctx))
}

// SetParams sets the total set of ibc-transfer parameters.
// SetParams sets the total set of pfm parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
5 changes: 5 additions & 0 deletions middleware/packet-forward-middleware/router/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
// InitGenesis performs genesis initialization for the ibc-router module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
err := am.ValidateGenesis(cdc, nil, data)
if err != nil {
panic(err)
}

var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
Expand Down
7 changes: 3 additions & 4 deletions middleware/packet-forward-middleware/router/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package types

// NewGenesisState creates a 29-fee GenesisState instance.
// NewGenesisState creates a pfm GenesisState instance.
func NewGenesisState(params Params, inFlightPackets map[string]InFlightPacket) *GenesisState {
return &GenesisState{
Params: params,
InFlightPackets: inFlightPackets,
}
}

// DefaultGenesisState returns a GenesisState with "transfer" as the default PortID.
// DefaultGenesisState returns a GenesisState with a default fee percentage of 0.
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
InFlightPackets: make(map[string]InFlightPacket),
}
}

// Validate performs basic genesis state validation returning an error upon any
// failure.
// Validate performs basic genesis state validation returning an error upon any failure.
func (gs GenesisState) Validate() error {
return gs.Params.Validate()
}
15 changes: 9 additions & 6 deletions middleware/packet-forward-middleware/router/types/params.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
package types

import (
fmt "fmt"
"fmt"

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

var (
// DefaultFeePercentage is the default value used to extract a fee from all forwarded packets.
DefaultFeePercentage = sdk.NewDec(0)

// KeyFeePercentage is store's key for FeePercentage Params
KeyFeePercentage = []byte("FeePercentage")
)

// ParamKeyTable type declaration for parameters
// ParamKeyTable type declaration for parameters.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// NewParams creates a new parameter configuration for the ibc transfer module
// NewParams creates a new parameter configuration for the pfm module.
func NewParams(feePercentage sdk.Dec) Params {
return Params{
FeePercentage: feePercentage,
}
}

// DefaultParams is the default parameter configuration for the ibc-transfer module
// DefaultParams is the default parameter configuration for the pfm module.
func DefaultParams() Params {
return NewParams(DefaultFeePercentage)
}

// Validate all ibc-transfer module parameters
// Validate the pfm module parameters.
func (p Params) Validate() error {
return validateFeePercentage(p.FeePercentage)
}

// ParamSetPairs implements params.ParamSet
// ParamSetPairs implements params.ParamSet.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyFeePercentage, p.FeePercentage, validateFeePercentage),
}
}

// validateFeePercentage asserts that the fee percentage param is a valid sdk.Dec type.
func validateFeePercentage(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
Expand Down

0 comments on commit 5bb6779

Please sign in to comment.