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: Implement v2 Acknowledgement rpc handler #7452

Merged
merged 24 commits into from
Oct 14, 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
23 changes: 12 additions & 11 deletions modules/core/04-channel/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ func (k *Keeper) Acknowledgement(ctx context.Context, msg *channeltypesv2.MsgAck
return nil, errorsmod.Wrap(err, "acknowledge packet verification failed")
}

_ = relayer
recvResults := make(map[string]channeltypesv2.RecvPacketResult)
for _, r := range msg.Acknowledgement.AcknowledgementResults {
recvResults[r.AppName] = r.RecvPacketResult
}

// TODO: implement once app router is wired up.
// https://github.com/cosmos/ibc-go/issues/7384
// for _, pd := range msg.PacketData {
// cbs := k.PortKeeper.AppRouter.Route(pd.SourcePort)
// err := cbs.OnSendPacket(ctx, msg.SourceId, sequence, msg.TimeoutTimestamp, pd, signer)
// if err != nil {
// return nil, err
// }
// }
for _, pd := range msg.Packet.Data {
cbs := k.Router.Route(pd.SourcePort)
err := cbs.OnAcknowledgementPacket(ctx, msg.Packet.SourceChannel, msg.Packet.DestinationChannel, pd, recvResults[pd.DestinationPort].Acknowledgement, relayer)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed OnAcknowledgementPacket for source port %s, source channel %s, destination channel %s", pd.SourcePort, msg.Packet.SourceChannel, msg.Packet.DestinationChannel)
}
}

return nil, nil
return &channeltypesv2.MsgAcknowledgementResponse{Result: channeltypesv1.SUCCESS}, nil
}

// RecvPacket implements the PacketMsgServer RecvPacket method.
Expand Down
132 changes: 131 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 @@ -2,6 +2,7 @@ package keeper_test

import (
"context"
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -286,7 +287,6 @@ func (suite *KeeperTestSuite) TestProvideCounterparty() {
},
nil,
},

{
"failure: signer does not match creator",
func() {
Expand Down Expand Up @@ -337,3 +337,133 @@ func (suite *KeeperTestSuite) TestProvideCounterparty() {
}
}
}

func (suite *KeeperTestSuite) TestMsgAcknowledgement() {
var (
path *ibctesting.Path
msgAckPacket *channeltypesv2.MsgAcknowledgement
recvPacket channeltypesv2.Packet
)
testCases := []struct {
name string
malleate func()
expError error
}{
{
name: "success",
malleate: func() {},
},
{
name: "success: NoOp",
malleate: func() {
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetPacketCommitment(suite.chainA.GetContext(), recvPacket.SourceChannel, recvPacket.Sequence, []byte{})

// Modify the callback to return an error.
// This way, we can verify that the callback is not executed in a No-op case.
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnAcknowledgementPacket = func(context.Context, string, string, channeltypesv2.PacketData, []byte, sdk.AccAddress) error {
return errors.New("OnAcknowledgementPacket callback failed")
}
},
},
{
name: "failure: invalid signer",
malleate: func() {
msgAckPacket.Signer = ""
},
expError: errors.New("empty address string is not allowed"),
},
{
name: "failure: callback fails",
malleate: func() {
path.EndpointA.Chain.GetSimApp().MockModuleV2A.IBCApp.OnAcknowledgementPacket = func(context.Context, string, string, channeltypesv2.PacketData, []byte, sdk.AccAddress) error {
return errors.New("OnAcknowledgementPacket callback failed")
}
},
expError: errors.New("OnAcknowledgementPacket callback failed"),
},
{
name: "failure: counterparty not found",
malleate: func() {
// change the source id to a non-existent channel.
msgAckPacket.Packet.SourceChannel = "not-existent-channel"
},
expError: channeltypesv2.ErrChannelNotFound,
},
{
name: "failure: invalid commitment",
malleate: func() {
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetPacketCommitment(suite.chainA.GetContext(), recvPacket.SourceChannel, recvPacket.Sequence, []byte("foo"))
},
expError: channeltypesv2.ErrInvalidPacket,
},
{
name: "failure: failed membership verification",
malleate: func() {
msgAckPacket.ProofHeight = clienttypes.ZeroHeight()
},
expError: errors.New("failed packet acknowledgement verification"),
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()

path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupV2()

timeoutTimestamp := suite.chainA.GetTimeoutTimestamp()

// Send packet from A to B
msgSendPacket := channeltypesv2.NewMsgSendPacket(path.EndpointA.ChannelID, timeoutTimestamp, suite.chainA.SenderAccount.GetAddress().String(), mockv2.NewMockPacketData(mockv2.ModuleNameA, mockv2.ModuleNameB))
res, err := path.EndpointA.Chain.SendMsgs(msgSendPacket)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NoError(path.EndpointB.UpdateClient())

// Receive packet on B
recvPacket = channeltypesv2.NewPacket(1, path.EndpointA.ChannelID, path.EndpointB.ChannelID, timeoutTimestamp, mockv2.NewMockPacketData(mockv2.ModuleNameA, mockv2.ModuleNameB))
// get proof of packet commitment from chainA
packetKey := hostv2.PacketCommitmentKey(recvPacket.SourceChannel, recvPacket.Sequence)
proof, proofHeight := path.EndpointA.QueryProof(packetKey)

// Construct msgRecvPacket to be sent to B
msgRecvPacket := channeltypesv2.NewMsgRecvPacket(recvPacket, proof, proofHeight, suite.chainB.SenderAccount.GetAddress().String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] maybe add some spacing in between the different sections, send/recv/ack etc.

res, err = suite.chainB.SendMsgs(msgRecvPacket)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NoError(path.EndpointA.UpdateClient())

// Construct expected acknowledgement
ack := channeltypesv2.Acknowledgement{
AcknowledgementResults: []channeltypesv2.AcknowledgementResult{
{
AppName: mockv2.ModuleNameB,
RecvPacketResult: channeltypesv2.RecvPacketResult{
Status: channeltypesv2.PacketStatus_Success,
Acknowledgement: mock.MockPacketData,
},
},
},
}

// Consttruct MsgAcknowledgement
packetKey = hostv2.PacketAcknowledgementKey(recvPacket.DestinationChannel, recvPacket.Sequence)
proof, proofHeight = path.EndpointB.QueryProof(packetKey)
msgAckPacket = channeltypesv2.NewMsgAcknowledgement(recvPacket, ack, proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String())

tc.malleate()

// Finally, acknowledge the packet on A
res, err = suite.chainA.SendMsgs(msgAckPacket)

expPass := tc.expError == nil

if expPass {
suite.Require().NoError(err)
suite.NotNil(res)
} else {
ibctesting.RequireErrorIsOrContains(suite.T(), err, tc.expError, "expected error %q, got %q instead", tc.expError, err)
}
})
}
}
11 changes: 11 additions & 0 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ func NewMsgRecvPacket(packet Packet, proofCommitment []byte, proofHeight clientt
Signer: signer,
}
}

// NewMsgAcknowledgement creates a new MsgAcknowledgement instance
func NewMsgAcknowledgement(packet Packet, acknowledgement Acknowledgement, proofAcked []byte, proofHeight clienttypes.Height, signer string) *MsgAcknowledgement {
return &MsgAcknowledgement{
Packet: packet,
Acknowledgement: acknowledgement,
ProofAcked: proofAcked,
ProofHeight: proofHeight,
Signer: signer,
}
}
1 change: 1 addition & 0 deletions testing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func UnmarshalMsgResponses(cdc codec.Codec, data []byte, msgs ...codec.ProtoMars
// RequireErrorIsOrContains verifies that the passed error is either a target error or contains its error message.
func RequireErrorIsOrContains(t *testing.T, err, targetError error, msgAndArgs ...interface{}) {
t.Helper()
require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this was removed in the recv PR, we don't need this since errors.Is accounts for a nil value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that if errors.Is fails, we go to the second part of the || and we try to call .Error() on err, causing a panic.

This only happens when the test is broken and we are checking for an error that doesn't happen, so unlikely to be checked in on main, but I think it's still worth having it.

require.True(
t,
errors.Is(err, targetError) ||
Expand Down
Loading