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: reusable metadata validation #729

Merged
merged 24 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e13b08c
define and generate new metadata proto type
damiannolan Jan 7, 2022
a5e9947
refactor types pkg, remove version string parsers, add PortPrefix
damiannolan Jan 7, 2022
a79ab75
refactor ica entrypoint and handshake to handle connection ids in met…
damiannolan Jan 7, 2022
f14d4dd
fixing broken test cases
damiannolan Jan 7, 2022
b8b0cbb
adding controller port and metadata validation, adding new testcases
damiannolan Jan 7, 2022
d5380ee
updating proto doc and removing commented out code
damiannolan Jan 7, 2022
873d8cd
Merge branch 'main' into damian/615-refactor-ica-connection-ids
damiannolan Jan 7, 2022
07a506d
updating testcase names, adding metadata constructor func, updating P…
damiannolan Jan 10, 2022
dd95566
Merge branch 'main' into damian/615-refactor-ica-connection-ids
damiannolan Jan 10, 2022
264b307
adding ErrInvalidControllerPort and ErrInvalidHostPort
damiannolan Jan 10, 2022
777ab3b
Merge branch 'damian/615-refactor-ica-connection-ids' of github.com:c…
damiannolan Jan 10, 2022
4267799
refactoring metadata validation to reusable func
damiannolan Jan 10, 2022
6ef8997
returning correct err type
damiannolan Jan 10, 2022
15356d5
Merge branch 'main' into damian/615-followup
damiannolan Jan 13, 2022
dc73e95
regenerating protos after merge conflicts
damiannolan Jan 13, 2022
3257aa4
adding separate validation funcs for controller and host
damiannolan Jan 13, 2022
9dc5386
correcting error msg in ValidateHostMetadata
damiannolan Jan 13, 2022
3a99f1b
updating with review suggestions
damiannolan Jan 13, 2022
cb74934
Merge branch 'main' into damian/615-followup
damiannolan Jan 13, 2022
2214791
adding additional empty address check to ACK step, adding test case
damiannolan Jan 13, 2022
8d34265
adding strings.Trimspace
damiannolan Jan 13, 2022
69411d0
adding success with empty address testcase for ValidateHostMetadata
damiannolan Jan 14, 2022
f321d07
Merge branch 'main' into damian/615-followup
damiannolan Jan 14, 2022
d72b263
Merge branch 'main' into damian/615-followup
damiannolan Jan 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
connectiontypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types"
)
Expand Down Expand Up @@ -47,14 +46,10 @@ func (k Keeper) OnChanOpenInit(
return sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata")
}

if err := k.validateConnectionParams(ctx, connectionHops, metadata.ControllerConnectionId, metadata.HostConnectionId); err != nil {
if err := icatypes.ValidateControllerMetadata(ctx, k.channelKeeper, connectionHops, metadata); err != nil {
return err
}

if metadata.Version != icatypes.Version {
return sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "expected %s, got %s", icatypes.Version, metadata.Version)
}

activeChannelID, found := k.GetActiveChannelID(ctx, portID)
if found {
return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "existing active channel %s for portID %s", activeChannelID, portID)
Expand Down Expand Up @@ -84,12 +79,17 @@ func (k Keeper) OnChanOpenAck(
return sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata")
}

if err := icatypes.ValidateAccountAddress(metadata.Address); err != nil {
channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID)
if !found {
return sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "failed to retrieve channel %s on port %s", channelID, portID)
}

if err := icatypes.ValidateControllerMetadata(ctx, k.channelKeeper, channel.ConnectionHops, metadata); err != nil {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if metadata.Version != icatypes.Version {
return sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "expected %s, got %s", icatypes.Version, metadata.Version)
if strings.TrimSpace(metadata.Address) == "" {
return sdkerrors.Wrap(icatypes.ErrInvalidAccountAddress, "interchain account address cannot be empty")
}

k.SetActiveChannelID(ctx, portID, channelID)
Expand All @@ -109,22 +109,3 @@ func (k Keeper) OnChanCloseConfirm(

return nil
}

// validateConnectionParams asserts the provided controller and host connection identifiers match that of the associated connection stored in state
func (k Keeper) validateConnectionParams(ctx sdk.Context, connectionHops []string, controllerConnectionID, hostConnectionID string) error {
connectionID := connectionHops[0]
connection, err := k.channelKeeper.GetConnection(ctx, connectionID)
if err != nil {
return err
}

if controllerConnectionID != connectionID {
return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", connectionID, controllerConnectionID)
}

if hostConnectionID != connection.GetCounterparty().GetConnectionID() {
return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", connection.GetCounterparty().GetConnectionID(), hostConnectionID)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ func (suite *KeeperTestSuite) TestOnChanOpenAck() {
},
false,
},
{
"empty account address",
func() {
metadata.Address = ""

versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata)
suite.Require().NoError(err)

path.EndpointA.Counterparty.ChannelConfig.Version = string(versionBytes)
},
false,
},
{
"invalid counterparty version",
func() {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 20 additions & 22 deletions modules/apps/27-interchain-accounts/controller/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 1 addition & 25 deletions modules/apps/27-interchain-accounts/host/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
connectiontypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
)
Expand Down Expand Up @@ -44,14 +43,10 @@ func (k Keeper) OnChanOpenTry(
return "", sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata")
}

if err := k.validateConnectionParams(ctx, connectionHops, metadata.ControllerConnectionId, metadata.HostConnectionId); err != nil {
if err := icatypes.ValidateHostMetadata(ctx, k.channelKeeper, connectionHops, metadata); err != nil {
return "", err
}

if metadata.Version != icatypes.Version {
return "", sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "expected %s, got %s", icatypes.Version, metadata.Version)
}

// On the host chain the capability may only be claimed during the OnChanOpenTry
// The capability being claimed in OpenInit is for a controller chain (the port is different)
if err := k.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
Expand Down Expand Up @@ -95,22 +90,3 @@ func (k Keeper) OnChanCloseConfirm(

return nil
}

// validateConnectionParams asserts the provided controller and host connection identifiers match that of the associated connection stored in state
func (k Keeper) validateConnectionParams(ctx sdk.Context, connectionHops []string, controllerConnectionID, hostConnectionID string) error {
connectionID := connectionHops[0]
connection, err := k.channelKeeper.GetConnection(ctx, connectionID)
if err != nil {
return err
}

if hostConnectionID != connectionID {
return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", connectionID, controllerConnectionID)
}

if controllerConnectionID != connection.GetCounterparty().GetConnectionID() {
return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", connection.GetCounterparty().GetConnectionID(), hostConnectionID)
}

return nil
}
40 changes: 19 additions & 21 deletions modules/apps/27-interchain-accounts/host/types/host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading