-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 (cherry picked from commit 94d0840) # Conflicts: # modules/apps/27-interchain-accounts/controller/types/msgs.go # testing/values.go
- Loading branch information
1 parent
d8d0a7e
commit de520ed
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
modules/apps/27-interchain-accounts/controller/types/msgs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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{ | ||
ConnectionId: connectionID, | ||
Owner: owner, | ||
Version: version, | ||
} | ||
} | ||
|
||
// 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 { | ||
accAddr, err := sdk.AccAddressFromBech32(msg.Owner) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return []sdk.AccAddress{accAddr} | ||
} |
96 changes: 96 additions & 0 deletions
96
modules/apps/27-interchain-accounts/controller/types/msgs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters