Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Feb 5, 2025
1 parent b5e40de commit 5217f33
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
17 changes: 16 additions & 1 deletion modules/core/04-channel/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,13 @@ func (suite *KeeperTestSuite) TestMsgAcknowledgement() {
testCases := []struct {
name string
malleate func()
payload types.Payload
expError error
}{
{
name: "success",
malleate: func() {},
payload: mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
},
{
name: "success: NoOp",
Expand All @@ -311,6 +313,15 @@ func (suite *KeeperTestSuite) TestMsgAcknowledgement() {
return mock.MockApplicationCallbackError
}
},
payload: mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
},
{
name: "success: failed receive",
malleate: func() {
// relayer can pass in an empty acknowledgement on failure
ack = types.Acknowledgement{}
},
payload: mockv2.NewErrorMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
},
{
name: "failure: callback fails",
Expand All @@ -319,6 +330,7 @@ func (suite *KeeperTestSuite) TestMsgAcknowledgement() {
return mock.MockApplicationCallbackError
}
},
payload: mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
expError: mock.MockApplicationCallbackError,
},
{
Expand All @@ -327,20 +339,23 @@ func (suite *KeeperTestSuite) TestMsgAcknowledgement() {
// change the source id to a non-existent channel.
packet.SourceClient = "not-existent-channel"
},
payload: mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
expError: clienttypes.ErrCounterpartyNotFound,
},
{
name: "failure: invalid commitment",
malleate: func() {
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetPacketCommitment(suite.chainA.GetContext(), packet.SourceClient, packet.Sequence, []byte("foo"))
},
payload: mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
expError: types.ErrInvalidPacket,
},
{
name: "failure: failed membership verification",
malleate: func() {
ack.AppAcknowledgements[0] = mock.MockFailPacketData
},
payload: mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB),
expError: errors.New("failed packet acknowledgement verification"),
},
}
Expand All @@ -355,7 +370,7 @@ func (suite *KeeperTestSuite) TestMsgAcknowledgement() {

var err error
// Send packet from A to B
packet, err = path.EndpointA.MsgSendPacket(timeoutTimestamp, mockv2.NewMockPayload(mockv2.ModuleNameA, mockv2.ModuleNameB))
packet, err = path.EndpointA.MsgSendPacket(timeoutTimestamp, tc.payload)
suite.Require().NoError(err)

err = path.EndpointB.MsgRecvPacket(packet)
Expand Down
8 changes: 3 additions & 5 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ func (msg *MsgAcknowledgement) ValidateBasic() error {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}

if err := msg.Packet.ValidateBasic(); err != nil {
return err
}

return msg.Acknowledgement.ValidateBasic()
// NOTE: Acknowledgement is allowed to be empty if the receive failed
// so we will check in the acknowledgement handler
return msg.Packet.ValidateBasic()
}

// NewMsgTimeout creates a new MsgTimeout instance
Expand Down
7 changes: 0 additions & 7 deletions modules/core/04-channel/v2/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,6 @@ func (s *TypesTestSuite) TestMsgAcknowledge_ValidateBasic() {
},
expError: types.ErrInvalidPacket,
},
{
name: "failure: invalid acknowledgement",
malleate: func() {
msg.Acknowledgement = types.NewAcknowledgement(true, []byte(""))
},
expError: types.ErrInvalidAcknowledgement,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
Expand Down
12 changes: 9 additions & 3 deletions testing/mock/v2/ibc_module.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package mock

import (
"bytes"
"context"

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

channeltypesv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
"github.com/cosmos/ibc-go/v9/modules/core/api"
mockv1 "github.com/cosmos/ibc-go/v9/testing/mock"
)

var _ api.IBCModule = (*IBCModule)(nil)
Expand Down Expand Up @@ -42,11 +44,15 @@ func (im IBCModule) OnSendPacket(ctx context.Context, sourceChannel string, dest
return nil
}

func (im IBCModule) OnRecvPacket(ctx context.Context, sourceChannel string, destinationChannel string, sequence uint64, data channeltypesv2.Payload, relayer sdk.AccAddress) channeltypesv2.RecvPacketResult {
func (im IBCModule) OnRecvPacket(ctx context.Context, sourceChannel string, destinationChannel string, sequence uint64, payload channeltypesv2.Payload, relayer sdk.AccAddress) channeltypesv2.RecvPacketResult {
if im.IBCApp.OnRecvPacket != nil {
return im.IBCApp.OnRecvPacket(ctx, sourceChannel, destinationChannel, sequence, data, relayer)
return im.IBCApp.OnRecvPacket(ctx, sourceChannel, destinationChannel, sequence, payload, relayer)
}
if bytes.Equal(mockv1.MockPacketData, payload.Value) {
return MockRecvPacketResult
} else {

Check failure on line 53 in testing/mock/v2/ibc_module.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 53 in testing/mock/v2/ibc_module.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return MockFailRecvPacketResult
}
return MockRecvPacketResult
}

func (im IBCModule) OnAcknowledgementPacket(ctx context.Context, sourceChannel string, destinationChannel string, sequence uint64, recvSuccess bool, acknowledgement []byte, payload channeltypesv2.Payload, relayer sdk.AccAddress) error {
Expand Down
12 changes: 11 additions & 1 deletion testing/mock/v2/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
Acknowledgement: mockv1.MockAcknowledgement.Acknowledgement(),
}
MockFailRecvPacketResult = channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Success,
Status: channeltypesv2.PacketStatus_Failure,
Acknowledgement: mockv1.MockFailAcknowledgement.Acknowledgement(),
}
)
Expand All @@ -30,3 +30,13 @@ func NewMockPayload(sourcePort, destPort string) channeltypesv2.Payload {
Version: mockv1.Version,
}
}

func NewErrorMockPayload(sourcePort, destPort string) channeltypesv2.Payload {
return channeltypesv2.Payload{
SourcePort: sourcePort,
DestinationPort: destPort,
Encoding: transfertypes.EncodingProtobuf,
Value: mockv1.MockFailPacketData,
Version: mockv1.Version,
}
}

0 comments on commit 5217f33

Please sign in to comment.