Skip to content

Commit 8977703

Browse files
committed
chore: add validation for MsgAcknowledgement.
1 parent 1682792 commit 8977703

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

modules/core/04-channel/v2/types/msgs.go

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
99
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
10+
commitmenttypesv1 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
1011
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
1112
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
1213
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
@@ -18,6 +19,12 @@ var (
1819

1920
_ sdk.Msg = (*MsgCreateChannel)(nil)
2021
_ sdk.HasValidateBasic = (*MsgCreateChannel)(nil)
22+
23+
_ sdk.Msg = (*MsgSendPacket)(nil)
24+
_ sdk.HasValidateBasic = (*MsgSendPacket)(nil)
25+
26+
_ sdk.Msg = (*MsgAcknowledgement)(nil)
27+
_ sdk.HasValidateBasic = (*MsgAcknowledgement)(nil)
2128
)
2229

2330
// NewMsgProvideCounterparty creates a new MsgProvideCounterparty instance
@@ -130,3 +137,19 @@ func NewMsgAcknowledgement(packet Packet, acknowledgement Acknowledgement, proof
130137
Signer: signer,
131138
}
132139
}
140+
141+
// ValidateBasic performs basic checks on a MsgAcknowledgement.
142+
func (msg *MsgAcknowledgement) ValidateBasic() error {
143+
if len(msg.ProofAcked) == 0 {
144+
return errorsmod.Wrap(commitmenttypesv1.ErrInvalidProof, "cannot submit an empty acknowledgement proof")
145+
}
146+
147+
// TODO: Add validation for ack object https://github.com/cosmos/ibc-go/issues/7472
148+
149+
_, err := sdk.AccAddressFromBech32(msg.Signer)
150+
if err != nil {
151+
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
152+
}
153+
154+
return msg.Packet.ValidateBasic()
155+
}

modules/core/04-channel/v2/types/msgs_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import (
66

77
"github.com/stretchr/testify/suite"
88

9+
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
910
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
1011
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
1112
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
1213
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
1314
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
1415
ibctesting "github.com/cosmos/ibc-go/v9/testing"
16+
mockv2 "github.com/cosmos/ibc-go/v9/testing/mock/v2"
1517
)
1618

19+
var testProof = []byte("proof")
20+
1721
type TypesTestSuite struct {
1822
suite.Suite
1923

@@ -211,3 +215,53 @@ func (s *TypesTestSuite) TestMsgSendPacketValidateBasic() {
211215
})
212216
}
213217
}
218+
219+
func (s *TypesTestSuite) TestMsgAcknowledge_ValidateBasic() {
220+
var msg *types.MsgAcknowledgement
221+
222+
testCases := []struct {
223+
name string
224+
malleate func()
225+
expError error
226+
}{
227+
{
228+
name: "success",
229+
malleate: func() {},
230+
},
231+
{
232+
name: "failure: invalid signer",
233+
malleate: func() {
234+
msg.Signer = ""
235+
},
236+
expError: ibcerrors.ErrInvalidAddress,
237+
},
238+
{
239+
name: "failure: invalid packet",
240+
malleate: func() {
241+
msg.Packet.Sequence = 0
242+
},
243+
expError: types.ErrInvalidPacket,
244+
},
245+
}
246+
for _, tc := range testCases {
247+
s.Run(tc.name, func() {
248+
msg = types.NewMsgAcknowledgement(
249+
types.NewPacket(1, ibctesting.FirstChannelID, ibctesting.SecondChannelID, s.chainA.GetTimeoutTimestamp(), mockv2.NewMockPacketData(mockv2.ModuleNameA, mockv2.ModuleNameB)),
250+
types.Acknowledgement{},
251+
testProof,
252+
clienttypes.ZeroHeight(),
253+
s.chainA.SenderAccount.GetAddress().String(),
254+
)
255+
256+
tc.malleate()
257+
258+
err := msg.ValidateBasic()
259+
expPass := tc.expError == nil
260+
if expPass {
261+
s.Require().NoError(err)
262+
} else {
263+
ibctesting.RequireErrorIsOrContains(s.T(), err, tc.expError)
264+
}
265+
})
266+
}
267+
}

modules/core/keeper/events_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper_test
22

33
import (
44
"testing"
5+
56
"github.com/stretchr/testify/require"
67

78
sdk "github.com/cosmos/cosmos-sdk/types"

modules/core/keeper/msg_server.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
errorsmod "cosmossdk.io/errors"
89

910
sdk "github.com/cosmos/cosmos-sdk/types"

modules/core/keeper/msg_server_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
upgradetypes "cosmossdk.io/x/upgrade/types"
89

910
sdk "github.com/cosmos/cosmos-sdk/types"

0 commit comments

Comments
 (0)