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

chore: adding sdk.Msg impl for ics27 MsgRegisterAccount #2081

Merged
merged 13 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
26 changes: 25 additions & 1 deletion modules/apps/27-interchain-accounts/controller/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package types

import (
"strings"

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

host "github.com/cosmos/ibc-go/v5/modules/core/24-host"
)

var _ sdk.Msg = &MsgRegisterAccount{}

// NewMsgRegisterAccount creates a new instance of MsgRegisterAccount
func NewMsgRegisterAccount(connectionID, owner, version string) *MsgRegisterAccount {
return &MsgRegisterAccount{
Expand All @@ -15,10 +22,27 @@ func NewMsgRegisterAccount(connectionID, owner, version string) *MsgRegisterAcco

// ValidateBasic implements sdk.Msg
func (msg MsgRegisterAccount) ValidateBasic() error {
if err := host.ConnectionIdentifierValidator(msg.ConnectionId); err != nil {
return sdkerrors.Wrap(err, "invalid connection ID")
}

if strings.TrimSpace(msg.Owner) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "owner address cannot be empty")
}

if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to parse owner address: %s", msg.Owner)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

do we also need a validation on the version?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we can since this version may be wrapped with many different versions (ics29 and so forth). There's no guarantee that the provided version is an ics27 version at the top level


return nil
}

// GetSigners implements sdk.Msg
func (msg MsgRegisterAccount) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{}
accAddr, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(err)
}

return []sdk.AccAddress{accAddr}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types"
ibctesting "github.com/cosmos/ibc-go/v5/testing"
)

var (
testAccAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs"
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit but maybe we can add this a const file?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added TestAccAddress to testing/values.go

testMetadataString = icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID)
)

func TestMsgRegisterAccountValidateBasic(t *testing.T) {
var msg *types.MsgRegisterAccount

testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {},
true,
},
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
{
"connection id is invalid",
func() {
msg.ConnectionId = ""
},
false,
},
{
"owner address is empty",
func() {
msg.Owner = ""
},
false,
},
{
"owner address is invalid",
func() {
msg.Owner = "invalid_address"
},
false,
},
}

for i, tc := range testCases {

msg = types.NewMsgRegisterAccount(ibctesting.FirstConnectionID, testAccAddress, testMetadataString)

tc.malleate()

err := msg.ValidateBasic()
if tc.expPass {
require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)
} else {
require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)
}
}
}

func TestMsgRegisterAccountGetSigners(t *testing.T) {
expSigner, err := sdk.AccAddressFromBech32(testAccAddress)
require.NoError(t, err)

msg := types.NewMsgRegisterAccount(ibctesting.FirstConnectionID, testAccAddress, testMetadataString)
require.Equal(t, []sdk.AccAddress{expSigner}, msg.GetSigners())
}