From 94d0840e0a14979c105d67f426954c7c146108ec Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Wed, 24 Aug 2022 15:32:16 +0200 Subject: [PATCH] chore: adding `sdk.Msg` impl for ics27 `MsgRegisterAccount` (#2081) * adding new controller msg service, register account types, register interfaces and boilerplate * fixing typo * fixing protodoc and regenerate godocs * adding channel id to MsgRegisterAccountResponse * adding sdk.Msg impl for MsgRegisterAccount * formatting imports * adding additional tests with multiple versions, creating TestAccAddress const --- .../controller/types/msgs.go | 26 ++++- .../controller/types/msgs_test.go | 96 +++++++++++++++++++ testing/values.go | 4 +- 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 modules/apps/27-interchain-accounts/controller/types/msgs_test.go diff --git a/modules/apps/27-interchain-accounts/controller/types/msgs.go b/modules/apps/27-interchain-accounts/controller/types/msgs.go index 9033df4bc3c..fec761e2123 100644 --- a/modules/apps/27-interchain-accounts/controller/types/msgs.go +++ b/modules/apps/27-interchain-accounts/controller/types/msgs.go @@ -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{ @@ -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) + } + 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} } diff --git a/modules/apps/27-interchain-accounts/controller/types/msgs_test.go b/modules/apps/27-interchain-accounts/controller/types/msgs_test.go new file mode 100644 index 00000000000..1fdd4f338a6 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/msgs_test.go @@ -0,0 +1,96 @@ +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" + feetypes "github.com/cosmos/ibc-go/v5/modules/apps/29-fee/types" + ibctesting "github.com/cosmos/ibc-go/v5/testing" +) + +func TestMsgRegisterAccountValidateBasic(t *testing.T) { + var msg *types.MsgRegisterAccount + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + { + "success: with empty channel version", + func() { + msg.Version = "" + }, + true, + }, + { + "success: with fee enabled channel version", + func() { + feeMetadata := feetypes.Metadata{ + FeeVersion: feetypes.Version, + AppVersion: icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID), + } + + bz := feetypes.ModuleCdc.MustMarshalJSON(&feeMetadata) + msg.Version = string(bz) + }, + true, + }, + { + "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, + ibctesting.TestAccAddress, + icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID), + ) + + 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(ibctesting.TestAccAddress) + require.NoError(t, err) + + msg := types.NewMsgRegisterAccount(ibctesting.FirstConnectionID, ibctesting.TestAccAddress, "") + require.Equal(t, []sdk.AccAddress{expSigner}, msg.GetSigners()) +} diff --git a/testing/values.go b/testing/values.go index e60de189e03..e9ee8c8afc7 100644 --- a/testing/values.go +++ b/testing/values.go @@ -48,7 +48,9 @@ var ( // DefaultTrustLevel sets params variables used to create a TM client DefaultTrustLevel = ibctm.DefaultTrustLevel - TestCoin = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) + + TestAccAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" + TestCoin = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) UpgradePath = []string{"upgrade", "upgradedIBCState"}