Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add validation for MsgAcknowledgement. #7473

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ var (
_ sdk.Msg = (*MsgCreateChannel)(nil)
_ sdk.HasValidateBasic = (*MsgCreateChannel)(nil)

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

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

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

// NewMsgProvideCounterparty creates a new MsgProvideCounterparty instance
Expand Down Expand Up @@ -149,6 +155,22 @@ func NewMsgAcknowledgement(packet Packet, acknowledgement Acknowledgement, proof
}
}

// ValidateBasic performs basic checks on a MsgAcknowledgement.
func (msg *MsgAcknowledgement) ValidateBasic() error {
if len(msg.ProofAcked) == 0 {
return errorsmod.Wrap(commitmenttypesv1.ErrInvalidProof, "cannot submit an empty acknowledgement proof")
}

// TODO: Add validation for ack object https://github.com/cosmos/ibc-go/issues/7472

_, 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()
}

// NewMsgTimeout creates a new MsgTimeout instance
func NewMsgTimeout(packet Packet, proofUnreceived []byte, proofHeight clienttypes.Height, signer string) *MsgTimeout {
return &MsgTimeout{
Expand Down
58 changes: 58 additions & 0 deletions modules/core/04-channel/v2/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/suite"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
Expand Down Expand Up @@ -268,3 +269,60 @@ func (s *TypesTestSuite) TestMsgRecvPacketValidateBasic() {
})
}
}

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

testCases := []struct {
name string
malleate func()
expError error
}{
{
name: "success",
malleate: func() {},
},
{
name: "failure: invalid proof of acknowledgement",
malleate: func() {
msg.ProofAcked = []byte{}
},
expError: commitmenttypes.ErrInvalidProof,
},
{
name: "failure: invalid signer",
malleate: func() {
msg.Signer = ""
},
expError: ibcerrors.ErrInvalidAddress,
},
{
name: "failure: invalid packet",
malleate: func() {
msg.Packet.Sequence = 0
},
expError: types.ErrInvalidPacket,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
msg = types.NewMsgAcknowledgement(
types.NewPacket(1, ibctesting.FirstChannelID, ibctesting.SecondChannelID, s.chainA.GetTimeoutTimestamp(), mockv2.NewMockPacketData(mockv2.ModuleNameA, mockv2.ModuleNameB)),
types.Acknowledgement{},
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)
}
})
}
}
Loading