Skip to content

Commit

Permalink
chore: adding encoding and txType fields to metadata (cosmos#824)
Browse files Browse the repository at this point in the history
* adding encoding and txTypes fields to ica metadata

* adapting metadata and validation to support encoding and txTypes fields. updating test cases

* renaming field to tx_type

* fixing failing tests from merge
  • Loading branch information
damiannolan authored Feb 2, 2022
1 parent bbcc09c commit 5ae8e35
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ See ICS004: https://github.com/cosmos/ibc/tree/master/spec/core/ics-004-channel-
| `controller_connection_id` | [string](#string) | | controller_connection_id is the connection identifier associated with the controller chain |
| `host_connection_id` | [string](#string) | | host_connection_id is the connection identifier associated with the host chain |
| `address` | [string](#string) | | address defines the interchain account address to be fulfilled upon the OnChanOpenTry handshake step NOTE: the address field is empty on the OnChanOpenInit handshake step |
| `encoding` | [string](#string) | | encoding defines the supported codec format |
| `tx_type` | [string](#string) | | tx_type defines the type of transactions the interchain account can execute |



Expand Down
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,
TxType: icatypes.TxTypeSDKMultiMsg,
}))
)

Expand Down Expand Up @@ -675,6 +677,8 @@ func (suite *InterchainAccountsTestSuite) TestSingleHostMultipleControllers() {
Version: icatypes.Version,
ControllerConnectionId: pathCToB.EndpointA.ConnectionID,
HostConnectionId: pathCToB.EndpointB.ConnectionID,
Encoding: icatypes.EncodingProtobuf,
TxType: icatypes.TxTypeSDKMultiMsg,
}))

err = SetupICAPath(pathCToB, TestOwnerAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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.TxType = "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.TxType = "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,
TxType: 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,
TxType: 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.TxType = "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,
TxType: 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, txType string) Metadata {
return Metadata{
Version: version,
ControllerConnectionId: controllerConnectionID,
HostConnectionId: hostConnectionID,
Address: accAddress,
Encoding: encoding,
TxType: txType,
}
}

// 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 !isSupportedTxType(metadata.TxType) {
return sdkerrors.Wrapf(ErrUnknownDataType, "unsupported transaction type %s", metadata.TxType)
}

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 !isSupportedTxType(metadata.TxType) {
return sdkerrors.Wrapf(ErrUnknownDataType, "unsupported transaction type %s", metadata.TxType)
}

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}
}

// isSupportedTxType returns true if the provided transaction type is supported, otherwise false
func isSupportedTxType(txType string) bool {
for _, t := range getSupportedTxTypes() {
if t == txType {
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 5ae8e35

Please sign in to comment.