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 ValidateBasic and test for MsgRecvPacket #7470

Merged
merged 4 commits into from
Oct 17, 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: 20 additions & 2 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/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 All @@ -18,6 +19,9 @@ var (

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

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

// NewMsgProvideCounterparty creates a new MsgProvideCounterparty instance
Expand Down Expand Up @@ -47,7 +51,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 @@ -120,6 +124,20 @@ func NewMsgRecvPacket(packet Packet, proofCommitment []byte, proofHeight clientt
}
}

// ValidateBasic performs basic checks on a MsgRecvPacket.
func (msg *MsgRecvPacket) ValidateBasic() error {
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved
if len(msg.ProofCommitment) == 0 {
return errorsmod.Wrap(commitmenttypesv1.ErrInvalidProof, "proof commitment 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()
}

// NewMsgAcknowledgement creates a new MsgAcknowledgement instance
func NewMsgAcknowledgement(packet Packet, acknowledgement Acknowledgement, proofAcked []byte, proofHeight clienttypes.Height, signer string) *MsgAcknowledgement {
return &MsgAcknowledgement{
Expand Down
57 changes: 57 additions & 0 deletions modules/core/04-channel/v2/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
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"
mockv2 "github.com/cosmos/ibc-go/v9/testing/mock/v2"
)

var testProof = []byte("test")

type TypesTestSuite struct {
suite.Suite

Expand Down Expand Up @@ -211,3 +214,57 @@ func (s *TypesTestSuite) TestMsgSendPacketValidateBasic() {
})
}
}

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 signer",
malleate: func() {
msg.Signer = ""
},
expError: ibcerrors.ErrInvalidAddress,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
packet := types.NewPacket(1, ibctesting.FirstChannelID, ibctesting.SecondChannelID, s.chainA.GetTimeoutTimestamp(), mockv2.NewMockPacketData(mockv2.ModuleNameA, mockv2.ModuleNameB))

msg = types.NewMsgRecvPacket(packet, testProof, 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)
}
})
}
}
1 change: 1 addition & 0 deletions modules/core/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
1 change: 1 addition & 0 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
1 change: 1 addition & 0 deletions modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"

upgradetypes "cosmossdk.io/x/upgrade/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
Loading