From 3fe32f17de23f8180506e3c64653758f169aa898 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Fri, 30 Jun 2023 06:11:27 +0200 Subject: [PATCH 1/5] add function RelayPacketWithResults --- testing/path.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/testing/path.go b/testing/path.go index 4fb605ecbe8..b81a87c4d70 100644 --- a/testing/path.go +++ b/testing/path.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ) @@ -83,3 +84,54 @@ func (path *Path) RelayPacket(packet channeltypes.Packet) error { return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet") } + +// RelayPacketWithResults attempts to relay the packet first on EndpointA and then on EndpointB +// if EndpointA does not contain a packet commitment for that packet. The function returns: +// - The result of the packet receive transaction. +// - The acknowledgement written on the receiving chain. +// - An error if a relay step fails or the packet commitment does not exist on either endpoint. +func (path *Path) RelayPacketWithResults(packet channeltypes.Packet) (res *sdk.Result, ack []byte, err error) { + pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App.AppCodec(), packet)) { + + // packet found, relay from A to B + if err := path.EndpointB.UpdateClient(); err != nil { + return nil, nil, err + } + + res, err := path.EndpointB.RecvPacketWithResult(packet) + if err != nil { + return nil, nil, err + } + + ack, err := ParseAckFromEvents(res.GetEvents()) + if err != nil { + return res, ack, err + } + + return res, ack, path.EndpointA.AcknowledgePacket(packet, ack) + } + + pc = path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.App.AppCodec(), packet)) { + + // packet found, relay B to A + if err := path.EndpointA.UpdateClient(); err != nil { + return nil, nil, err + } + + res, err := path.EndpointA.RecvPacketWithResult(packet) + if err != nil { + return nil, nil, err + } + + ack, err := ParseAckFromEvents(res.GetEvents()) + if err != nil { + return res, ack, err + } + + return res, ack, path.EndpointB.AcknowledgePacket(packet, ack) + } + + return nil, nil, fmt.Errorf("packet commitment does not exist on either endpoint for provided packet") +} From 34afb3063e47202ade84447d9042b3519cda1ae2 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Fri, 30 Jun 2023 06:15:56 +0200 Subject: [PATCH 2/5] lint fix --- testing/path.go | 1 + 1 file changed, 1 insertion(+) diff --git a/testing/path.go b/testing/path.go index b81a87c4d70..28f56ca6784 100644 --- a/testing/path.go +++ b/testing/path.go @@ -5,6 +5,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ) From ea822980e8eb9ca1fa62859bedb4488225f7801c Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Fri, 30 Jun 2023 09:04:45 +0200 Subject: [PATCH 3/5] review comment --- testing/path.go | 45 ++------------------------------------------- 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/testing/path.go b/testing/path.go index 28f56ca6784..c57e5cb5ed3 100644 --- a/testing/path.go +++ b/testing/path.go @@ -41,49 +41,8 @@ func (path *Path) SetChannelOrdered() { // if EndpointA does not contain a packet commitment for that packet. An error is returned // if a relay step fails or the packet commitment does not exist on either endpoint. func (path *Path) RelayPacket(packet channeltypes.Packet) error { - pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App.AppCodec(), packet)) { - - // packet found, relay from A to B - if err := path.EndpointB.UpdateClient(); err != nil { - return err - } - - res, err := path.EndpointB.RecvPacketWithResult(packet) - if err != nil { - return err - } - - ack, err := ParseAckFromEvents(res.GetEvents()) - if err != nil { - return err - } - - return path.EndpointA.AcknowledgePacket(packet, ack) - } - - pc = path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.App.AppCodec(), packet)) { - - // packet found, relay B to A - if err := path.EndpointA.UpdateClient(); err != nil { - return err - } - - res, err := path.EndpointA.RecvPacketWithResult(packet) - if err != nil { - return err - } - - ack, err := ParseAckFromEvents(res.GetEvents()) - if err != nil { - return err - } - - return path.EndpointB.AcknowledgePacket(packet, ack) - } - - return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet") + _, _, err := path.RelayPacketWithResults(packet) + return err } // RelayPacketWithResults attempts to relay the packet first on EndpointA and then on EndpointB From 11332f3e91eee3dfbe8313b3f464c65588ba3329 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Mon, 3 Jul 2023 10:53:59 +0200 Subject: [PATCH 4/5] apply review suggestion --- testing/path.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/path.go b/testing/path.go index c57e5cb5ed3..794a4e146a7 100644 --- a/testing/path.go +++ b/testing/path.go @@ -50,7 +50,7 @@ func (path *Path) RelayPacket(packet channeltypes.Packet) error { // - The result of the packet receive transaction. // - The acknowledgement written on the receiving chain. // - An error if a relay step fails or the packet commitment does not exist on either endpoint. -func (path *Path) RelayPacketWithResults(packet channeltypes.Packet) (res *sdk.Result, ack []byte, err error) { +func (path *Path) RelayPacketWithResults(packet channeltypes.Packet) (*sdk.Result, []byte, error) { pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App.AppCodec(), packet)) { From 74cb4b65c0e434e4be6c7cdfa655d8fc977409f5 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Mon, 3 Jul 2023 12:53:33 +0200 Subject: [PATCH 5/5] review comments --- testing/path.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/testing/path.go b/testing/path.go index 794a4e146a7..082c1afb611 100644 --- a/testing/path.go +++ b/testing/path.go @@ -66,10 +66,14 @@ func (path *Path) RelayPacketWithResults(packet channeltypes.Packet) (*sdk.Resul ack, err := ParseAckFromEvents(res.GetEvents()) if err != nil { - return res, ack, err + return nil, nil, err + } + + if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil { + return nil, nil, err } - return res, ack, path.EndpointA.AcknowledgePacket(packet, ack) + return res, ack, nil } pc = path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) @@ -87,10 +91,14 @@ func (path *Path) RelayPacketWithResults(packet channeltypes.Packet) (*sdk.Resul ack, err := ParseAckFromEvents(res.GetEvents()) if err != nil { - return res, ack, err + return nil, nil, err + } + + if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil { + return nil, nil, err } - return res, ack, path.EndpointB.AcknowledgePacket(packet, ack) + return res, ack, nil } return nil, nil, fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")