Skip to content

Commit

Permalink
chore: add ValidateBasic and test for MsgRecvPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
bznein committed Oct 15, 2024
1 parent ef74ad6 commit 14ef24a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
26 changes: 24 additions & 2 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
commitmenttypesv1 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func (msg *MsgProvideCounterparty) ValidateBasic() error {
}

// NewMsgCreateChannel creates a new MsgCreateChannel instance
func NewMsgCreateChannel(clientID string, merklePathPrefix commitmenttypes.MerklePath, signer string) *MsgCreateChannel {
func NewMsgCreateChannel(clientID string, merklePathPrefix commitmenttypesv2.MerklePath, signer string) *MsgCreateChannel {
return &MsgCreateChannel{
Signer: signer,
ClientId: clientID,
Expand Down Expand Up @@ -91,6 +92,27 @@ func NewMsgRecvPacket(packet Packet, proofCommitment []byte, proofHeight clientt
}
}

// ValidateBasic performs basic checks on a MsgRecvPacket.
func (msg *MsgRecvPacket) ValidateBasic() error {
if err := msg.Packet.ValidateBasic(); err != nil {
return err
}

if len(msg.ProofCommitment) == 0 {
return errorsmod.Wrap(commitmenttypesv1.ErrInvalidProof, "proof commitment can not be empty")
}

if msg.ProofHeight.IsZero() {
return errorsmod.Wrap(clienttypes.ErrInvalidHeight, "proof height can not be zero")
}

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

// NewMsgAcknowledgement creates a new MsgAcknowledgement instance
func NewMsgAcknowledgement(packet Packet, acknowledgement Acknowledgement, proofAcked []byte, proofHeight clienttypes.Height, signer string) *MsgAcknowledgement {
return &MsgAcknowledgement{
Expand Down
73 changes: 73 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,11 +6,13 @@ import (

"github.com/stretchr/testify/suite"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/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"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
"github.com/cosmos/ibc-go/v9/testing/mock"
)

type TypesTestSuite struct {
Expand All @@ -19,6 +21,8 @@ type TypesTestSuite struct {
coordinator *ibctesting.Coordinator
chainA *ibctesting.TestChain
chainB *ibctesting.TestChain

proof []byte
}

func (s *TypesTestSuite) SetupTest() {
Expand Down Expand Up @@ -142,3 +146,72 @@ func (s *TypesTestSuite) TestMsgCreateChannelValidateBasic() {
}
}
}

func (s *TypesTestSuite) TestMsgRecvPacketValidateBasic() {
var msg *types.MsgRecvPacket
testCases := []struct {
name string
malleate func()
expError error
}{
{
name: "success",
malleate: func() {},
},
{
name: "failure: invalid packet",
malleate: func() {
msg.Packet.Data = []types.PacketData{}
},
expError: types.ErrInvalidPacket,
},
{
name: "failure: invalid proof commitment",
malleate: func() {
msg.ProofCommitment = []byte{}
},
expError: commitmenttypes.ErrInvalidProof,
},
{
name: "failure: invalid proof height",
malleate: func() {
msg.ProofHeight = clienttypes.ZeroHeight()
},
expError: clienttypes.ErrInvalidHeight,
},
{
name: "failure: invalid signer",
malleate: func() {
msg.Signer = ""
},
expError: ibcerrors.ErrInvalidAddress,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
packet := types.NewPacket(1,
ibctesting.FirstChannelID, ibctesting.FirstChannelID,
s.chainA.GetTimeoutTimestamp(),
types.PacketData{
SourcePort: ibctesting.MockPort,
DestinationPort: ibctesting.MockPort,
Payload: types.NewPayload("ics20-1", "json", mock.MockPacketData),
},
)

msg = types.NewMsgRecvPacket(packet, []byte("foo"), s.chainA.GetTimeoutHeight(), 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 14ef24a

Please sign in to comment.