Skip to content

Commit

Permalink
updating Version -> VersionPrefix, error msgs, validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Sep 17, 2021
1 parent d6725e1 commit 32240ff
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpart
return sdkerrors.Wrap(err, "unable to bind to newly generated portID")
}

msg := channeltypes.NewMsgChannelOpenInit(portID, types.Version, channeltypes.ORDERED, []string{connectionID}, types.PortID, types.ModuleName)
msg := channeltypes.NewMsgChannelOpenInit(portID, types.VersionPrefix, channeltypes.ORDERED, []string{connectionID}, types.PortID, types.ModuleName)
handler := k.msgRouter.Handler(msg)
if _, err := handler(ctx, msg); err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions modules/apps/27-interchain-accounts/keeper/handshake.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
Expand Down Expand Up @@ -91,8 +89,8 @@ func (k Keeper) OnChanOpenTry(

accAddr := types.GenerateAddress(counterparty.PortId)
parsedAddr := types.ParseAddressFromVersion(version)
if strings.Compare(parsedAddr, accAddr.String()) != 0 {
return sdkerrors.Wrapf(types.ErrInvalidAccountAddress, "invalid account address: %s, expected %s", parsedAddr, accAddr)
if parsedAddr != accAddr.String() {
return sdkerrors.Wrapf(types.ErrInvalidAccountAddress, "version contains invalid account address: expected %s, got %s", parsedAddr, accAddr)
}

// Register interchain account if it does not already exist
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenInit() {
Ordering: channeltypes.ORDERED,
Counterparty: counterparty,
ConnectionHops: []string{path.EndpointA.ConnectionID},
Version: types.Version,
Version: types.VersionPrefix,
}

chanCap, err = suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(portID, path.EndpointA.ChannelID))
Expand Down Expand Up @@ -149,7 +149,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenTry() {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path = NewICAPath(suite.chainA, suite.chainB)
counterpartyVersion = types.Version
counterpartyVersion = types.VersionPrefix
suite.coordinator.SetupConnections(path)

err := InitInterchainAccount(path.EndpointA, TestOwnerAddress)
Expand Down
6 changes: 4 additions & 2 deletions modules/apps/27-interchain-accounts/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
var (
// TestOwnerAddress defines a reusable bech32 address for testing purposes
TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs"
// TestPortID defines a resuable port identifier for testing purposes
TestPortID = fmt.Sprintf("ics-27-0-0-%s", TestOwnerAddress)
// TestVersion defines a resuable interchainaccounts version string for testing purposes
TestVersion = fmt.Sprintf("%s|%s", types.Version, types.GenerateAddress("ics-27-0-0-"+TestOwnerAddress))
TestVersion = fmt.Sprintf("%s|%s", types.VersionPrefix, types.GenerateAddress(TestPortID))
)

type KeeperTestSuite struct {
Expand All @@ -43,7 +45,7 @@ func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {
path.EndpointB.ChannelConfig.PortID = types.PortID
path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED
path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED
path.EndpointA.ChannelConfig.Version = types.Version
path.EndpointA.ChannelConfig.Version = types.VersionPrefix
path.EndpointB.ChannelConfig.Version = TestVersion

return path
Expand Down
6 changes: 3 additions & 3 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ func (am AppModule) NegotiateAppVersion(
counterparty channeltypes.Counterparty,
proposedVersion string,
) (string, error) {
if proposedVersion != types.Version {
return "", sdkerrors.Wrapf(types.ErrInvalidVersion, "failed to negotiate app version: expected %s, got %s", types.Version, proposedVersion)
if proposedVersion != types.VersionPrefix {
return "", sdkerrors.Wrapf(types.ErrInvalidVersion, "failed to negotiate app version: expected %s, got %s", types.VersionPrefix, proposedVersion)
}

return fmt.Sprint(types.Version, types.Delimiter, types.GenerateAddress(counterparty.PortId)), nil
return types.NewAppVersion(types.GenerateAddress(counterparty.PortId).String()), nil
}
9 changes: 6 additions & 3 deletions modules/apps/27-interchain-accounts/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
var (
// TestOwnerAddress defines a reusable bech32 address for testing purposes
TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs"
// TestPortID defines a resuable port identifier for testing purposes
TestPortID = fmt.Sprintf("ics-27-0-0-%s", TestOwnerAddress)
// TestVersion defines a resuable interchainaccounts version string for testing purposes
TestVersion = fmt.Sprintf("%s|%s", types.Version, types.GenerateAddress("ics-27-0-0-"+TestOwnerAddress))
TestVersion = fmt.Sprintf("%s|%s", types.VersionPrefix, types.GenerateAddress(TestPortID))
)

type InterchainAccountsTestSuite struct {
Expand All @@ -42,7 +44,7 @@ func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {
path.EndpointB.ChannelConfig.PortID = types.PortID
path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED
path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED
path.EndpointA.ChannelConfig.Version = types.Version
path.EndpointA.ChannelConfig.Version = types.VersionPrefix
path.EndpointB.ChannelConfig.Version = TestVersion

return path
Expand Down Expand Up @@ -71,7 +73,8 @@ func (suite *InterchainAccountsTestSuite) TestNegotiateAppVersion() {
ChannelId: path.EndpointB.ChannelID,
}

version, err := cbs.NegotiateAppVersion(suite.chainA.GetContext(), channeltypes.ORDERED, path.EndpointA.ConnectionID, types.PortID, *counterparty, types.Version)
version, err := cbs.NegotiateAppVersion(suite.chainA.GetContext(), channeltypes.ORDERED, path.EndpointA.ConnectionID, types.PortID, *counterparty, types.VersionPrefix)
suite.Require().NoError(err)
suite.Require().NoError(types.ValidateVersion(version))
suite.Require().Equal(TestVersion, version)
}
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func GenerateAddress(portID string) sdk.AccAddress {

// ParseAddressFromVersion trims the interchainaccounts version prefix and returns the associated account address
func ParseAddressFromVersion(version string) string {
return strings.TrimPrefix(version, fmt.Sprint(Version, Delimiter))
return strings.TrimPrefix(version, fmt.Sprint(VersionPrefix, Delimiter))
}

// GeneratePortID generates the portID for a specific owner
Expand Down
30 changes: 16 additions & 14 deletions modules/apps/27-interchain-accounts/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,44 @@ import (
)

const (
// ModuleName defines the Interchain Account module name
// ModuleName defines the interchain accounts module name
ModuleName = "interchainaccounts"

// Version defines the current version the IBC interchainaccounts
// module supports
Version = "ics27-1"
// VersionPrefix defines the current version for interchain accounts
VersionPrefix = "ics27-1"

// PortID is the default port id that the interchainaccounts module binds to
// PortID is the default port id that the interchain accounts module binds to
PortID = "ibcaccount"

// StoreKey is the store key string for IBC interchainaccounts
// StoreKey is the store key string for interchain accounts
StoreKey = ModuleName

// RouterKey is the message route for IBC interchainaccounts
// RouterKey is the message route for interchain accounts
RouterKey = ModuleName

// QuerierRoute is the querier route for IBC interchainaccounts
// QuerierRoute is the querier route for interchain accounts
QuerierRoute = ModuleName

// Delimiter is the delimiter used for the interchainaccounts version string
// Delimiter is the delimiter used for the interchain accounts version string
Delimiter = "|"
)

var (
// PortKey defines the key to store the port ID in store
PortKey = "portID"
PortKey = []byte{0x01}
)

// NewVersion returns a complete version string in the format: VersionPrefix + Delimter + AccAddress
func NewAppVersion(addr string) string {
return fmt.Sprint(VersionPrefix, Delimiter, addr)
}

// KeyActiveChannel creates and returns a new key used for active channels store operations
func KeyActiveChannel(portId string) []byte {
return []byte(fmt.Sprintf("activeChannel/%s", portId))
}

// KeyOwnerAccount creates and returns a new key used for owner account store operations
func KeyOwnerAccount(portId string) []byte {
return []byte(fmt.Sprintf("owner/%s", portId))
}

func GetIdentifier(portID, channelID string) string {
return fmt.Sprintf("%s/%s/", portID, channelID)
}
18 changes: 9 additions & 9 deletions modules/apps/27-interchain-accounts/types/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import (
)

// DefaultMaxAddrLength defines the default maximum character length used in validation of addresses
var DefaultMaxAddrLength = 64

// DefaultMinAddrLength defines the default minimum character length used in validation of addresses
var DefaultMinAddrLength = 32
var DefaultMaxAddrLength = 128

// IsValidAddr defines a regular expression to check if the provided string consists of
// strictly alphanumeric characters
Expand All @@ -22,16 +19,19 @@ var IsValidAddr = regexp.MustCompile("^[a-zA-Z0-9]*$").MatchString
func ValidateVersion(version string) error {
s := strings.Split(version, Delimiter)

if s[0] != Version {
return sdkerrors.Wrapf(ErrInvalidVersion, "invalid version: expected %s, got %s", Version, s[0])
if s[0] != VersionPrefix {
return sdkerrors.Wrapf(ErrInvalidVersion, "expected %s, got %s", VersionPrefix, s[0])
}

if len(s) > 1 {
if !IsValidAddr(s[1]) || len(s[1]) > DefaultMaxAddrLength || len(s[1]) < DefaultMinAddrLength {
if len(s) != 2 {
return sdkerrors.Wrap(ErrInvalidAccountAddress, "unexpected address format")
}

if !IsValidAddr(s[1]) || len(s[1]) == 0 || len(s[1]) > DefaultMaxAddrLength {
return sdkerrors.Wrapf(
ErrInvalidAccountAddress,
"address must contain strictly alphanumeric characters, %d-%d characters in length",
DefaultMinAddrLength,
"address must contain strictly alphanumeric characters, not exceeding %d characters in length",
DefaultMaxAddrLength,
)
}
Expand Down
19 changes: 12 additions & 7 deletions modules/apps/27-interchain-accounts/types/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func (suite *TypesTestSuite) TestValidateVersion() {
}{
{
"success",
fmt.Sprint(types.Version, types.Delimiter, TestOwnerAddress),
fmt.Sprint(types.VersionPrefix, types.Delimiter, TestOwnerAddress),

This comment has been minimized.

Copy link
@colin-axner

colin-axner Sep 17, 2021

Contributor

super small nit, these can use NewAppVersion

This comment has been minimized.

Copy link
@damiannolan

damiannolan Sep 17, 2021

Author Member

100%

I'm going to update the NewAppVersion func sig to take the 3 args instead of one (accAddr), will be cleaner imo.
Should be good to go then 👍

I'll follow up on the module accounts stuff after this PR.

edit: done!

true,
},
{
"success - version only",
fmt.Sprint(types.Version),
fmt.Sprint(types.VersionPrefix),
true,
},
{
Expand All @@ -28,18 +28,23 @@ func (suite *TypesTestSuite) TestValidateVersion() {
false,
},
{
"invalid account address - 31 chars",
fmt.Sprint(types.Version, types.Delimiter, "xtignpvthxbwxtmnzyfwhhywobaatlt"),
"invalid account address - empty",
fmt.Sprint(types.VersionPrefix, types.Delimiter, ""),
false,
},
{
"invalid account address - 65 chars",
fmt.Sprint(types.Version, types.Delimiter, "ofwafxhdmqcdbpzvrccxkidbunrwyyoboyctignpvthxbwxtmnzyfwhhywobaatlt"),
"invalid account address - exceeded character length",
fmt.Sprint(types.VersionPrefix, types.Delimiter, "ofwafxhdmqcdbpzvrccxkidbunrwyyoboyctignpvthxbwxtmnzyfwhhywobaatltfwafxhdmqcdbpzvrccxkidbunrwyyoboyctignpvthxbwxtmnzyfwhhywobaatlt"),
false,
},
{
"invalid account address - non alphanumeric characters",
fmt.Sprint(types.Version, types.Delimiter, "-_-"),
fmt.Sprint(types.VersionPrefix, types.Delimiter, "-_-"),
false,
},
{
"invalid account address - address contains additional delimiter",
fmt.Sprint(types.VersionPrefix, types.Delimiter, "cosmos17dtl0mjt3t77kpu|hg2edqzjpszulwhgzuj9ljs"),
false,
},
}
Expand Down

0 comments on commit 32240ff

Please sign in to comment.