Skip to content

Commit

Permalink
chore: adding MsgChannelUpgradeInit fields and sdk.Msg interface (#1837)
Browse files Browse the repository at this point in the history
* update re: comments

* update re: protos

* whitespace

* update error
  • Loading branch information
charleenfei committed Aug 5, 2022
1 parent 33a6571 commit 690feee
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 4 deletions.
10 changes: 10 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3144,6 +3144,16 @@ MsgChannelUpgradeConfirmResponse defines the MsgChannelUpgradeConfirm response t
MsgChanelUpgradeInit defines the request type for the ChannelUpgradeInit rpc


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `port_id` | [string](#string) | | |
| `channel_id` | [string](#string) | | |
| `proposed_upgrade_channel` | [Channel](#ibc.core.channel.v1.Channel) | | |
| `timeout_height` | [ibc.core.client.v1.Height](#ibc.core.client.v1.Height) | | |
| `timeout_timestamp` | [uint64](#uint64) | | |
| `signer` | [string](#string) | | |





Expand Down
43 changes: 39 additions & 4 deletions modules/core/04-channel/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,18 +499,53 @@ var _ sdk.Msg = &MsgChannelUpgradeInit{}

// NewMsgChannelUpgradeInit constructs a new MsgChannelUpgradeInit
// nolint:interfacer
func NewMsgChannelUpgradeInit() *MsgChannelUpgradeInit {
return &MsgChannelUpgradeInit{}
func NewMsgChannelUpgradeInit(
portID, channelID string,
proposedUpgradeChannel Channel,
TimeoutHeight clienttypes.Height,
TimeoutTimestamp uint64,
signer string,
) *MsgChannelUpgradeInit {
return &MsgChannelUpgradeInit{
PortId: portID,
ChannelId: channelID,
ProposedUpgradeChannel: proposedUpgradeChannel,
TimeoutHeight: TimeoutHeight,
TimeoutTimestamp: TimeoutTimestamp,
Signer: signer,
}
}

// ValidateBasic implements sdk.Msg
func (msg MsgChannelUpgradeInit) ValidateBasic() error {
return errors.New("error method not implemented")
if err := host.PortIdentifierValidator(msg.PortId); err != nil {
return sdkerrors.Wrap(err, "invalid port ID")
}
if !IsValidChannelID(msg.ChannelId) {
return ErrInvalidChannelIdentifier
}
if msg.ProposedUpgradeChannel.State != INITUPGRADE {
return sdkerrors.Wrapf(ErrInvalidChannelState, "expected: %s, got: %s", INITUPGRADE, msg.ProposedUpgradeChannel.State)
}
if msg.TimeoutHeight.IsZero() && msg.TimeoutTimestamp == 0 {
return sdkerrors.Wrap(ErrInvalidUpgradeTimeout, "timeout height and timeout timestamp cannot both be 0")
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}

return nil
}

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

return []sdk.AccAddress{signer}
}

var _ sdk.Msg = &MsgChannelUpgradeTry{}
Expand Down
74 changes: 74 additions & 0 deletions modules/core/04-channel/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,80 @@ func (suite *TypesTestSuite) TestMsgAcknowledgementValidateBasic() {
}
}

func (suite *TypesTestSuite) TestMsgChannelUpgradeInitValidateBasic() {
var msg *types.MsgChannelUpgradeInit

testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {},
true,
},
{
"invalid port identifier",
func() {
msg.PortId = invalidPort
},
false,
},
{
"invalid channel identifier",
func() {
msg.ChannelId = invalidChannel
},
false,
},
{
"invalid proposed upgrade channel state",
func() {
msg.ProposedUpgradeChannel.State = types.TRYUPGRADE
},
false,
},
{
"timeout height is zero && timeout timestamp is zero",
func() {
msg.TimeoutHeight = clienttypes.ZeroHeight()
msg.TimeoutTimestamp = 0
},
false,
},
{
"missing signer address",
func() {
msg.Signer = emptyAddr
},
false,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
msg = types.NewMsgChannelUpgradeInit(
ibctesting.MockPort, ibctesting.FirstChannelID,
types.Channel{State: types.INITUPGRADE},
clienttypes.NewHeight(0, 10000),
0,
addr,
)

tc.malleate()
err := msg.ValidateBasic()

if tc.expPass {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
}
})
}
}

func (suite *TypesTestSuite) TestMsgChannelUpgradeTryValidateBasic() {
var msg *types.MsgChannelUpgradeTry

Expand Down
Loading

0 comments on commit 690feee

Please sign in to comment.