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

refactor(genesis): improve error handling by removing panics #7921

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/apps/27-interchain-accounts/host/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// InitGenesis initializes the interchain accounts host application state from a provided genesis state
func InitGenesis(ctx context.Context, keeper Keeper, state genesistypes.HostGenesisState) {
func InitGenesis(ctx context.Context, keeper Keeper, state genesistypes.HostGenesisState) error {
keeper.setPort(ctx, state.Port)

for _, ch := range state.ActiveChannels {
Expand All @@ -21,9 +21,10 @@ func InitGenesis(ctx context.Context, keeper Keeper, state genesistypes.HostGene
}

if err := state.Params.Validate(); err != nil {
panic(fmt.Errorf("could not set ica host params at genesis: %v", err))
return fmt.Errorf("could not set ica host params at genesis: %w", err)
}
keeper.SetParams(ctx, state.Params)
return nil
}

// ExportGenesis returns the interchain accounts host exported genesis
Expand Down
6 changes: 6 additions & 0 deletions modules/apps/transfer/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

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

host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
Expand Down Expand Up @@ -29,6 +31,10 @@
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {

Check failure on line 34 in modules/apps/transfer/types/genesis.go

View workflow job for this annotation

GitHub Actions / lint

gs.Params.Validate undefined (type Params has no field or method Validate)) (typecheck)

Check failure on line 34 in modules/apps/transfer/types/genesis.go

View workflow job for this annotation

GitHub Actions / lint

gs.Params.Validate undefined (type Params has no field or method Validate)) (typecheck)

Check failure on line 34 in modules/apps/transfer/types/genesis.go

View workflow job for this annotation

GitHub Actions / lint

gs.Params.Validate undefined (type Params has no field or method Validate)) (typecheck)

Check failure on line 34 in modules/apps/transfer/types/genesis.go

View workflow job for this annotation

GitHub Actions / lint

gs.Params.Validate undefined (type Params has no field or method Validate)) (typecheck)

Check failure on line 34 in modules/apps/transfer/types/genesis.go

View workflow job for this annotation

GitHub Actions / lint

gs.Params.Validate undefined (type Params has no field or method Validate)) (typecheck)
return fmt.Errorf("invalid transfer genesis state parameters: %w", err)
}

if err := host.PortIdentifierValidator(gs.PortId); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// state.
func InitGenesis(ctx context.Context, k *keeper.Keeper, gs types.GenesisState) error {
if err := gs.Params.Validate(); err != nil {
panic(fmt.Errorf("invalid ibc client genesis state parameters: %v", err))
return fmt.Errorf("invalid ibc client genesis state parameters: %w", err)
}
k.SetParams(ctx, gs.Params)

Expand Down
5 changes: 3 additions & 2 deletions modules/core/04-channel/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

// InitGenesis initializes the ibc channel submodule's state from a provided genesis
// state.
func InitGenesis(ctx context.Context, k *keeper.Keeper, gs types.GenesisState) {
func InitGenesis(ctx context.Context, k *keeper.Keeper, gs types.GenesisState) error {
if err := gs.Params.Validate(); err != nil {
panic(fmt.Sprintf("invalid ibc channel genesis state parameters: %v", err))
return fmt.Errorf("invalid ibc channel genesis state parameters: %w", err)
}
k.SetParams(ctx, gs.Params)
for _, channel := range gs.Channels {
Expand All @@ -38,6 +38,7 @@ func InitGenesis(ctx context.Context, k *keeper.Keeper, gs types.GenesisState) {
k.SetNextSequenceAck(ctx, as.PortId, as.ChannelId, as.Sequence)
}
k.SetNextChannelSequence(ctx, gs.NextChannelSequence)
return nil
}

// ExportGenesis returns the ibc channel submodule's exported genesis.
Expand Down
4 changes: 4 additions & 0 deletions modules/core/04-channel/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func DefaultGenesisState() GenesisState {
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {
return fmt.Errorf("invalid genesis state params: %w", err)
}

// keep track of the max sequence to ensure it is less than
// the next sequence used in creating connection identifiers.
var maxSequence uint64
Expand Down
3 changes: 2 additions & 1 deletion modules/core/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func RandomizedGenState(simState *module.SimulationState) {

bz, err := json.MarshalIndent(&ibcGenesis, "", " ")
if err != nil {
panic(err)
fmt.Printf("Error marshaling IBC genesis state: %v\n", err)
return
}
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", ibcexported.ModuleName, bz)
simState.GenState[ibcexported.ModuleName] = simState.Cdc.MustMarshalJSON(&ibcGenesis)
Expand Down
8 changes: 4 additions & 4 deletions modules/light-clients/08-wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ func (k Keeper) InitGenesis(ctx context.Context, gs types.GenesisState) error {

// ExportGenesis returns the 08-wasm module's exported genesis. This includes the code
// for all contracts previously stored.
func (k Keeper) ExportGenesis(ctx context.Context) types.GenesisState {
func (k Keeper) ExportGenesis(ctx context.Context) (types.GenesisState, error) {
checksums, err := k.GetAllChecksums(ctx)
if err != nil {
panic(err)
return types.GenesisState{}, err
}

// Grab code from wasmVM and add to genesis state.
var genesisState types.GenesisState
for _, checksum := range checksums {
code, err := k.GetVM().GetCode(checksum)
if err != nil {
panic(err)
return types.GenesisState{}, err
}
genesisState.Contracts = append(genesisState.Contracts, types.Contract{
CodeBytes: code,
})
}

return genesisState
return genesisState, nil
}
Loading