Skip to content

Commit

Permalink
chore: add ValidateBasic for MsgSendPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
bznein committed Oct 15, 2024
1 parent ef74ad6 commit 7749a90
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/core/04-channel/v2/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (
ErrInvalidPacket = errorsmod.Register(SubModuleName, 4, "invalid packet")
ErrInvalidPayload = errorsmod.Register(SubModuleName, 5, "invalid payload")
ErrSequenceSendNotFound = errorsmod.Register(SubModuleName, 6, "sequence send not found")
ErrInvalidPacketData = errorsmod.Register(SubModuleName, 7, "invalid packet data")
)
29 changes: 29 additions & 0 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

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"
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 @@ -81,6 +82,34 @@ func NewMsgSendPacket(sourceChannel string, timeoutTimestamp uint64, signer stri
}
}

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

if msg.TimeoutTimestamp == 0 {
return errorsmod.Wrap(channeltypesv1.ErrInvalidTimeout, "timeout must not be 0")
}

if len(msg.PacketData) != 1 {
return errorsmod.Wrapf(ErrInvalidPacketData, "packet data must be of length 1, got %d instead", len(msg.PacketData))
}

for _, pd := range msg.PacketData {
if err := pd.ValidateBasic(); err != nil {
return err
}
}

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

return nil
}

// NewMsgRecvPacket creates a new MsgRecvPacket instance.
func NewMsgRecvPacket(packet Packet, proofCommitment []byte, proofHeight clienttypes.Height, signer string) *MsgRecvPacket {
return &MsgRecvPacket{
Expand Down
69 changes: 69 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"

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"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
Expand Down Expand Up @@ -142,3 +143,71 @@ func (s *TypesTestSuite) TestMsgCreateChannelValidateBasic() {
}
}
}

func (s *TypesTestSuite) TestMsgSendPacketValidateBasic() {
var msg *types.MsgSendPacket
testCases := []struct {
name string
malleate func()
expError error
}{
{
name: "success",
malleate: func() {},
},
{
name: "failure: invalid source channel",
malleate: func() {
msg.SourceChannel = ""
},
expError: host.ErrInvalidID,
},
{
name: "failure: invalid timestamp",
malleate: func() {
msg.TimeoutTimestamp = 0
},
expError: channeltypesv1.ErrInvalidTimeout,
},
{
name: "failure: invalid length for packetdata",
malleate: func() {
msg.PacketData = []types.PacketData{}
},
expError: types.ErrInvalidPacketData,
},
{
name: "failure: invalid packetdata",
malleate: func() {
msg.PacketData[0].DestinationPort = ""
},
expError: host.ErrInvalidID,
},
{
name: "failure: invalid signer",
malleate: func() {
msg.Signer = ""
},
expError: ibcerrors.ErrInvalidAddress,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
msg = types.NewMsgSendPacket(
ibctesting.FirstChannelID, s.chainA.GetTimeoutTimestamp(),
s.chainA.SenderAccount.GetAddress().String(),
types.PacketData{SourcePort: ibctesting.MockPort, DestinationPort: ibctesting.MockPort, Payload: types.NewPayload("ics20-1", "json", ibctesting.MockPacketData)},
)

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 7749a90

Please sign in to comment.