Skip to content

Commit

Permalink
chore: adding MsgChannelUpgradeConfirm fields and sdk.Msg interface i…
Browse files Browse the repository at this point in the history
…mpl (#1833)

* chore: adding MsgChannelUpgradeConfirm to proto.tx

* chore: adding validate basic

* chore: adding tests for MsgChannelUpgradeConfirmValidateBasic

* reordering testcases as per review

* updating NewMsgChannelUpgradeConfirm to list args on separate lines

Co-authored-by: Damian Nolan <damiannolan@gmail.com>
  • Loading branch information
chatton and damiannolan authored Aug 4, 2022
1 parent 7ddd452 commit 33a6571
Show file tree
Hide file tree
Showing 5 changed files with 677 additions and 141 deletions.
12 changes: 12 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3112,6 +3112,18 @@ MsgChannelUpgradeCancelResponse defines the MsgChannelUpgradeCancel response typ
MsgChannelUpgradeConfirm defines the request type for the ChannelUpgradeConfirm rpc


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `port_id` | [string](#string) | | |
| `channel_id` | [string](#string) | | |
| `counterparty_channel` | [Channel](#ibc.core.channel.v1.Channel) | | |
| `proof_channel` | [bytes](#bytes) | | |
| `proof_upgrade_error` | [bytes](#bytes) | | |
| `proof_upgrade_sequence` | [bytes](#bytes) | | |
| `proof_height` | [ibc.core.client.v1.Height](#ibc.core.client.v1.Height) | | |
| `signer` | [string](#string) | | |





Expand Down
63 changes: 59 additions & 4 deletions modules/core/04-channel/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,18 +656,73 @@ var _ sdk.Msg = &MsgChannelUpgradeConfirm{}

// NewMsgChannelUpgradeConfirm constructs a new MsgChannelUpgradeConfirm
// nolint:interfacer
func NewMsgChannelUpgradeConfirm() *MsgChannelUpgradeConfirm {
return &MsgChannelUpgradeConfirm{}
func NewMsgChannelUpgradeConfirm(
portID,
channelID string,
counterpartyChannel Channel,
proofChannel,
proofUpgradeError,
proofUpgradeSequence []byte,
proofHeight clienttypes.Height,
signer string,
) *MsgChannelUpgradeConfirm {
return &MsgChannelUpgradeConfirm{
PortId: portID,
ChannelId: channelID,
CounterpartyChannel: counterpartyChannel,
ProofChannel: proofChannel,
ProofUpgradeError: proofUpgradeError,
ProofUpgradeSequence: proofUpgradeSequence,
ProofHeight: proofHeight,
Signer: signer,
}
}

// ValidateBasic implements sdk.Msg
func (msg MsgChannelUpgradeConfirm) 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 len(msg.ProofChannel) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty channel proof")
}

if len(msg.ProofUpgradeError) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty upgrade error proof")
}

if len(msg.ProofUpgradeSequence) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty upgrade sequence proof")
}

if msg.ProofHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "proof height must be non-zero")
}

if msg.CounterpartyChannel.State != OPEN {
return sdkerrors.Wrapf(ErrInvalidChannelState, "expected: %s, got: %s", OPEN, msg.CounterpartyChannel.State)
}

_, 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 MsgChannelUpgradeConfirm) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{}
signer, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}

var _ sdk.Msg = &MsgChannelUpgradeTimeout{}
Expand Down
143 changes: 118 additions & 25 deletions modules/core/04-channel/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeTryValidateBasic() {
}
}

func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelValidateBasic() {
var msg *types.MsgChannelUpgradeCancel
func (suite *TypesTestSuite) TestMsgChannelUpgradeAckValidateBasic() {
var msg *types.MsgChannelUpgradeAck

testCases := []struct {
name string
Expand All @@ -584,9 +584,16 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelValidateBasic() {
false,
},
{
"cannot submit an empty proof",
"cannot submit an empty channel proof",
func() {
msg.ProofErrorReceipt = emptyProof
msg.ProofChannel = emptyProof
},
false,
},
{
"cannot submit an empty upgrade sequence proof",
func() {
msg.ProofUpgradeSequence = emptyProof
},
false,
},
Expand All @@ -598,9 +605,9 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelValidateBasic() {
false,
},
{
"invalid error receipt sequence",
"invalid counterparty channel state",
func() {
msg.ErrorReceipt.Sequence = 0
msg.CounterpartyChannel.State = types.INITUPGRADE
},
false,
},
Expand All @@ -616,7 +623,12 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelValidateBasic() {
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
msg = types.NewMsgChannelUpgradeCancel(ibctesting.MockPort, ibctesting.FirstChannelID, types.ErrorReceipt{Sequence: 1}, suite.proof, height, addr)
msg = types.NewMsgChannelUpgradeAck(
ibctesting.MockPort, ibctesting.FirstChannelID,
types.Channel{State: types.TRYUPGRADE},
suite.proof, suite.proof,
height, addr,
)

tc.malleate()
err := msg.ValidateBasic()
Expand All @@ -630,16 +642,8 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelValidateBasic() {
}
}

func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelGetSigners() {
expSigner, err := sdk.AccAddressFromBech32(addr)
suite.Require().NoError(err)

msg := types.NewMsgChannelUpgradeCancel(ibctesting.MockPort, ibctesting.FirstChannelID, types.ErrorReceipt{Sequence: 1}, suite.proof, height, addr)
suite.Require().Equal([]sdk.AccAddress{expSigner}, msg.GetSigners())
}

func (suite *TypesTestSuite) TestMsgChannelUpgradeAckValidateBasic() {
var msg *types.MsgChannelUpgradeAck
func (suite *TypesTestSuite) TestMsgChannelUpgradeConfirmValidateBasic() {
var msg *types.MsgChannelUpgradeConfirm

testCases := []struct {
name string
Expand All @@ -665,6 +669,13 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeAckValidateBasic() {
},
false,
},
{
"invalid counterparty channel state",
func() {
msg.CounterpartyChannel.State = types.TRYUPGRADE
},
false,
},
{
"cannot submit an empty channel proof",
func() {
Expand All @@ -673,23 +684,23 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeAckValidateBasic() {
false,
},
{
"cannot submit an empty upgrade sequence proof",
"cannot submit an empty upgrade error proof",
func() {
msg.ProofUpgradeSequence = emptyProof
msg.ProofUpgradeError = emptyProof
},
false,
},
{
"proof height must be > 0",
"cannot submit an empty upgrade sequence proof",
func() {
msg.ProofHeight = clienttypes.ZeroHeight()
msg.ProofUpgradeSequence = emptyProof
},
false,
},
{
"invalid counterparty channel state",
"proof height must be > 0",
func() {
msg.CounterpartyChannel.State = types.INITUPGRADE
msg.ProofHeight = clienttypes.ZeroHeight()
},
false,
},
Expand All @@ -705,9 +716,9 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeAckValidateBasic() {
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
msg = types.NewMsgChannelUpgradeAck(
msg = types.NewMsgChannelUpgradeConfirm(
ibctesting.MockPort, ibctesting.FirstChannelID,
types.Channel{State: types.TRYUPGRADE},
types.Channel{State: types.OPEN}, suite.proof,
suite.proof, suite.proof,
height, addr,
)
Expand All @@ -723,3 +734,85 @@ func (suite *TypesTestSuite) TestMsgChannelUpgradeAckValidateBasic() {
})
}
}

func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelValidateBasic() {
var msg *types.MsgChannelUpgradeCancel

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,
},
{
"cannot submit an empty proof",
func() {
msg.ProofErrorReceipt = emptyProof
},
false,
},
{
"proof height must be > 0",
func() {
msg.ProofHeight = clienttypes.ZeroHeight()
},
false,
},
{
"invalid error receipt sequence",
func() {
msg.ErrorReceipt.Sequence = 0
},
false,
},
{
"missing signer address",
func() {
msg.Signer = emptyAddr
},
false,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
msg = types.NewMsgChannelUpgradeCancel(ibctesting.MockPort, ibctesting.FirstChannelID, types.ErrorReceipt{Sequence: 1}, suite.proof, height, addr)

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

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

func (suite *TypesTestSuite) TestMsgChannelUpgradeCancelGetSigners() {
expSigner, err := sdk.AccAddressFromBech32(addr)
suite.Require().NoError(err)

msg := types.NewMsgChannelUpgradeCancel(ibctesting.MockPort, ibctesting.FirstChannelID, types.ErrorReceipt{Sequence: 1}, suite.proof, height, addr)
suite.Require().Equal([]sdk.AccAddress{expSigner}, msg.GetSigners())
}
Loading

0 comments on commit 33a6571

Please sign in to comment.