Skip to content

Commit

Permalink
ica: genesis state validation (#554)
Browse files Browse the repository at this point in the history
* adding genesis state validation

* adding genesis state validation tests

* Update modules/apps/27-interchain-accounts/types/genesis_test.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* Update modules/apps/27-interchain-accounts/types/genesis_test.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* adding ValidateAccountAddress helper to reduce code duplication

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
  • Loading branch information
damiannolan and colin-axner committed Nov 29, 2021
1 parent 009cbec commit 5bed1d7
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 2 deletions.
8 changes: 7 additions & 1 deletion modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ica

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,7 +57,12 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {

// ValidateGenesis performs genesis state validation for the IBC interchain acounts module
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
return nil // TODO: https://github.com/cosmos/ibc-go/issues/535
var gs types.GenesisState
if err := cdc.UnmarshalJSON(bz, &gs); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}

return gs.Validate()
}

// RegisterRESTRoutes implements AppModuleBasic interface
Expand Down
77 changes: 77 additions & 0 deletions modules/apps/27-interchain-accounts/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

import (
host "github.com/cosmos/ibc-go/v2/modules/core/24-host"
)

// DefaultGenesis creates and returns the interchain accounts GenesisState
func DefaultGenesis() *GenesisState {
return &GenesisState{
Expand All @@ -16,6 +20,19 @@ func NewGenesisState(controllerGenesisState ControllerGenesisState, hostGenesisS
}
}

// Validate performs basic validation of the interchain accounts GenesisState
func (gs GenesisState) Validate() error {
if err := gs.ControllerGenesisState.Validate(); err != nil {
return err
}

if err := gs.HostGenesisState.Validate(); err != nil {
return err
}

return nil
}

// DefaultControllerGenesis creates and returns the default interchain accounts ControllerGenesisState
func DefaultControllerGenesis() ControllerGenesisState {
return ControllerGenesisState{}
Expand All @@ -30,6 +47,37 @@ func NewControllerGenesisState(channels []ActiveChannel, accounts []RegisteredIn
}
}

// Validate performs basic validation of the ControllerGenesisState
func (gs ControllerGenesisState) Validate() error {
for _, ch := range gs.ActiveChannels {
if err := host.ChannelIdentifierValidator(ch.ChannelId); err != nil {
return err
}

if err := host.PortIdentifierValidator(ch.PortId); err != nil {
return err
}
}

for _, acc := range gs.InterchainAccounts {
if err := host.PortIdentifierValidator(acc.PortId); err != nil {
return err
}

if err := ValidateAccountAddress(acc.AccountAddress); err != nil {
return err
}
}

for _, port := range gs.Ports {
if err := host.PortIdentifierValidator(port); err != nil {
return err
}
}

return nil
}

// DefaultHostGenesis creates and returns the default interchain accounts HostGenesisState
func DefaultHostGenesis() HostGenesisState {
return HostGenesisState{
Expand All @@ -45,3 +93,32 @@ func NewHostGenesisState(channels []ActiveChannel, accounts []RegisteredIntercha
Port: port,
}
}

// Validate performs basic validation of the HostGenesisState
func (gs HostGenesisState) Validate() error {
for _, ch := range gs.ActiveChannels {
if err := host.ChannelIdentifierValidator(ch.ChannelId); err != nil {
return err
}

if err := host.PortIdentifierValidator(ch.PortId); err != nil {
return err
}
}

for _, acc := range gs.InterchainAccounts {
if err := host.PortIdentifierValidator(acc.PortId); err != nil {
return err
}

if err := ValidateAccountAddress(acc.AccountAddress); err != nil {
return err
}
}

if err := host.PortIdentifierValidator(gs.Port); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 5bed1d7

Please sign in to comment.