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

ica: genesis state validation #554

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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)
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
}

return gs.Validate()
}

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

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

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 +22,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 +49,41 @@ 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 !IsValidAddr(acc.AccountAddress) || len(acc.AccountAddress) == 0 || len(acc.AccountAddress) > DefaultMaxAddrLength {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should make a InterchainAccAddressValidator function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

return sdkerrors.Wrapf(
ErrInvalidAccountAddress,
"address must contain strictly alphanumeric characters, not exceeding %d characters in length",
DefaultMaxAddrLength,
)
}
}

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 +99,36 @@ 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 !IsValidAddr(acc.AccountAddress) || len(acc.AccountAddress) == 0 || len(acc.AccountAddress) > DefaultMaxAddrLength {
return sdkerrors.Wrapf(
ErrInvalidAccountAddress,
"address must contain strictly alphanumeric characters, not exceeding %d characters in length",
DefaultMaxAddrLength,
)
}
}

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

return nil
}
Loading