Skip to content

Commit

Permalink
chore: add validation for MsgAcknowledgement.
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Oct 17, 2024
1 parent 1682792 commit 8977703
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
23 changes: 23 additions & 0 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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"
commitmenttypesv1 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/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 All @@ -18,6 +19,12 @@ var (

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

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

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

// NewMsgProvideCounterparty creates a new MsgProvideCounterparty instance
Expand Down Expand Up @@ -130,3 +137,19 @@ func NewMsgAcknowledgement(packet Packet, acknowledgement Acknowledgement, proof
Signer: signer,
}
}

// 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()
}
54 changes: 54 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,14 +6,18 @@ 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"
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("proof")

type TypesTestSuite struct {
suite.Suite

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

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

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

0 comments on commit 8977703

Please sign in to comment.