Skip to content

Commit

Permalink
chore: add ValidateBasic for MsgTimeout in ChannelKeeper V2 (#7482)
Browse files Browse the repository at this point in the history
* chore: add ValidateBasic for MsgTimeout in ChannelKeeper V2

* rename
  • Loading branch information
bznein authored Oct 18, 2024
1 parent 0a3b4be commit e77a0f4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
17 changes: 17 additions & 0 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
_ sdk.Msg = (*MsgRecvPacket)(nil)
_ sdk.HasValidateBasic = (*MsgRecvPacket)(nil)

_ sdk.Msg = (*MsgTimeout)(nil)
_ sdk.HasValidateBasic = (*MsgTimeout)(nil)

_ sdk.Msg = (*MsgAcknowledgement)(nil)
_ sdk.HasValidateBasic = (*MsgAcknowledgement)(nil)
)
Expand Down Expand Up @@ -180,3 +183,17 @@ func NewMsgTimeout(packet Packet, proofUnreceived []byte, proofHeight clienttype
Signer: signer,
}
}

// ValidateBasic performs basic checks on a MsgTimeout
func (msg *MsgTimeout) ValidateBasic() error {
if len(msg.ProofUnreceived) == 0 {
return errorsmod.Wrap(commitmenttypesv1.ErrInvalidProof, "proof unreceived can not be empty")
}

_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}

return msg.Packet.ValidateBasic()
}
57 changes: 56 additions & 1 deletion modules/core/04-channel/v2/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ func (s *TypesTestSuite) TestMsgRecvPacketValidateBasic() {

func (s *TypesTestSuite) TestMsgAcknowledge_ValidateBasic() {
var msg *types.MsgAcknowledgement

testCases := []struct {
name string
malleate func()
Expand Down Expand Up @@ -326,3 +325,59 @@ func (s *TypesTestSuite) TestMsgAcknowledge_ValidateBasic() {
})
}
}

func (s *TypesTestSuite) TestMsgTimeoutValidateBasic() {
var msg *types.MsgTimeout

testCases := []struct {
name string
malleate func()
expError error
}{
{
name: "success",
malleate: func() {},
},
{
name: "failure: invalid signer",
malleate: func() {
msg.Signer = ""
},
expError: ibcerrors.ErrInvalidAddress,
},
{
name: "failure: invalid packet",
malleate: func() {
msg.Packet.Sequence = 0
},
expError: types.ErrInvalidPacket,
},
{
name: "failure: invalid proof unreceived",
malleate: func() {
msg.ProofUnreceived = []byte{}
},
expError: commitmenttypes.ErrInvalidProof,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
msg = types.NewMsgTimeout(
types.NewPacket(1, ibctesting.FirstChannelID, ibctesting.SecondChannelID, s.chainA.GetTimeoutTimestamp(), mockv2.NewMockPacketData(mockv2.ModuleNameA, mockv2.ModuleNameB)),
testProof,
clienttypes.ZeroHeight(),
s.chainA.SenderAccount.GetAddress().String(),
)

tc.malleate()

err := msg.ValidateBasic()
expPass := tc.expError == nil
if expPass {
s.Require().NoError(err)
} else {
ibctesting.RequireErrorIsOrContains(s.T(), err, tc.expError)
}
})
}
}

0 comments on commit e77a0f4

Please sign in to comment.