Skip to content

Commit

Permalink
adapting metadata and validation to support encoding and txTypes fiel…
Browse files Browse the repository at this point in the history
…ds. updating test cases
  • Loading branch information
damiannolan committed Feb 2, 2022
1 parent 9a07154 commit c402145
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (
Version: icatypes.Version,
ControllerConnectionId: ibctesting.FirstConnectionID,
HostConnectionId: ibctesting.FirstConnectionID,
Encoding: icatypes.EncodingProtobuf,
TxTypes: icatypes.TxTypeSDKMultiMsg,
}))
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, connectionID, owner s
}

// NOTE: An empty string is provided for accAddress, to be fulfilled upon OnChanOpenTry handshake step
metadata := icatypes.NewMetadata(icatypes.Version, connectionID, connectionEnd.GetCounterparty().GetConnectionID(), "")
metadata := icatypes.NewMetadata(
icatypes.Version,
connectionID,
connectionEnd.GetCounterparty().GetConnectionID(),
"",
icatypes.EncodingProtobuf,
icatypes.TxTypeSDKMultiMsg,
)

versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ func (suite *KeeperTestSuite) TestOnChanOpenInit() {
},
false,
},
{
"unsupported encoding format",
func() {
metadata.Encoding = "invalid-encoding-format"

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

channel.Version = string(versionBytes)
path.EndpointA.SetChannel(*channel)
},
false,
},
{
"unsupported transaction type",
func() {
metadata.TxTypes = "invalid-tx-types"

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

channel.Version = string(versionBytes)
path.EndpointA.SetChannel(*channel)
},
false,
},
{
"connection not found",
func() {
Expand Down Expand Up @@ -144,7 +170,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenInit() {
path.EndpointA.ChannelConfig.PortID = portID

// default values
metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, "")
metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, "", icatypes.EncodingProtobuf, icatypes.TxTypeSDKMultiMsg)
versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata)
suite.Require().NoError(err)

Expand Down Expand Up @@ -211,6 +237,30 @@ func (suite *KeeperTestSuite) TestOnChanOpenAck() {
},
false,
},
{
"unsupported encoding format",
func() {
metadata.Encoding = "invalid-encoding-format"

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

path.EndpointA.Counterparty.ChannelConfig.Version = string(versionBytes)
},
false,
},
{
"unsupported transaction type",
func() {
metadata.TxTypes = "invalid-tx-types"

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

path.EndpointA.Counterparty.ChannelConfig.Version = string(versionBytes)
},
false,
},
{
"invalid account address",
func() {
Expand Down Expand Up @@ -275,7 +325,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenAck() {
err = path.EndpointB.ChanOpenTry()
suite.Require().NoError(err)

metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, TestAccAddress.String())
metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, TestAccAddress.String(), icatypes.EncodingProtobuf, icatypes.TxTypeSDKMultiMsg)
versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata)
suite.Require().NoError(err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
Version: icatypes.Version,
ControllerConnectionId: ibctesting.FirstConnectionID,
HostConnectionId: ibctesting.FirstConnectionID,
Encoding: icatypes.EncodingProtobuf,
TxTypes: icatypes.TxTypeSDKMultiMsg,
}))
)

Expand Down
2 changes: 2 additions & 0 deletions modules/apps/27-interchain-accounts/host/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
Version: icatypes.Version,
ControllerConnectionId: ibctesting.FirstConnectionID,
HostConnectionId: ibctesting.FirstConnectionID,
Encoding: icatypes.EncodingProtobuf,
TxTypes: icatypes.TxTypeSDKMultiMsg,
}))
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ func (suite *KeeperTestSuite) TestOnChanOpenTry() {
},
false,
},
{
"unsupported encoding format",
func() {
metadata.Encoding = "invalid-encoding-format"

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

path.EndpointA.ChannelConfig.Version = string(versionBytes)
},
false,
},
{
"unsupported transaction type",
func() {
metadata.TxTypes = "invalid-tx-types"

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

path.EndpointA.ChannelConfig.Version = string(versionBytes)
},
false,
},
{
"invalid controller connection ID",
func() {
Expand Down Expand Up @@ -141,7 +165,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenTry() {
path.EndpointB.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence)

// default values
metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, "")
metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, "", icatypes.EncodingProtobuf, icatypes.TxTypeSDKMultiMsg)
versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata)
suite.Require().NoError(err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
Version: icatypes.Version,
ControllerConnectionId: ibctesting.FirstConnectionID,
HostConnectionId: ibctesting.FirstConnectionID,
Encoding: icatypes.EncodingProtobuf,
TxTypes: icatypes.TxTypeSDKMultiMsg,
}))
)

Expand Down
60 changes: 59 additions & 1 deletion modules/apps/27-interchain-accounts/types/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@ import (
connectiontypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
)

const (
// EncodingProtobuf defines the protocol buffers proto3 encoding format
EncodingProtobuf = "proto3"

// TxTypeSDKMultiMsg defines the multi message transaction type supported by the Cosmos SDK
TxTypeSDKMultiMsg = "sdk_multi_msg"
)

// NewMetadata creates and returns a new ICS27 Metadata instance
func NewMetadata(version, controllerConnectionID, hostConnectionID, accAddress string) Metadata {
func NewMetadata(version, controllerConnectionID, hostConnectionID, accAddress, encoding, txTypes string) Metadata {
return Metadata{
Version: version,
ControllerConnectionId: controllerConnectionID,
HostConnectionId: hostConnectionID,
Address: accAddress,
Encoding: encoding,
TxTypes: txTypes,
}
}

// ValidateControllerMetadata performs validation of the provided ICS27 controller metadata parameters
func ValidateControllerMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connectionHops []string, metadata Metadata) error {
if !isSupportedEncoding(metadata.Encoding) {
return sdkerrors.Wrapf(ErrInvalidCodec, "unsupported encoding format %s", metadata.Encoding)
}

if !isSupportedTxTypes(metadata.TxTypes) {
return sdkerrors.Wrapf(ErrUnknownDataType, "unsupported transaction type %s", metadata.TxTypes)
}

connection, err := channelKeeper.GetConnection(ctx, connectionHops[0])
if err != nil {
return err
Expand All @@ -43,6 +61,14 @@ func ValidateControllerMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, co

// ValidateHostMetadata performs validation of the provided ICS27 host metadata parameters
func ValidateHostMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connectionHops []string, metadata Metadata) error {
if !isSupportedEncoding(metadata.Encoding) {
return sdkerrors.Wrapf(ErrInvalidCodec, "unsupported encoding format %s", metadata.Encoding)
}

if !isSupportedTxTypes(metadata.TxTypes) {
return sdkerrors.Wrapf(ErrUnknownDataType, "unsupported transaction type %s", metadata.TxTypes)
}

connection, err := channelKeeper.GetConnection(ctx, connectionHops[0])
if err != nil {
return err
Expand All @@ -65,6 +91,38 @@ func ValidateHostMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connecti
return nil
}

// isSupportedEncoding returns true if the provided encoding is supported, otherwise false
func isSupportedEncoding(encoding string) bool {
for _, enc := range getSupportedEncoding() {
if enc == encoding {
return true
}
}

return false
}

// getSupportedEncoding returns a string slice of supported encoding formats
func getSupportedEncoding() []string {
return []string{EncodingProtobuf}
}

// isSupportedTxTypes returns true if the provided transaction types are supported, otherwise false
func isSupportedTxTypes(txTypes string) bool {
for _, txType := range getSupportedTxTypes() {
if txType == txTypes {
return true
}
}

return false
}

// getSupportedTxTypes returns a string slice of supported transaction types
func getSupportedTxTypes() []string {
return []string{TxTypeSDKMultiMsg}
}

// validateConnectionParams compares the given the controller and host connection IDs to those set in the provided ICS27 Metadata
func validateConnectionParams(metadata Metadata, controllerConnectionID, hostConnectionID string) error {
if metadata.ControllerConnectionId != controllerConnectionID {
Expand Down
Loading

0 comments on commit c402145

Please sign in to comment.