From 53aae449e1cc8840c53f844ad3c841d0457734a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Fri, 28 Jan 2022 12:19:46 +0100 Subject: [PATCH 1/4] add endpoint.RecvPacketWithResult function, improve RelayPacket testing UX --- testing/endpoint.go | 24 ++++++++++++++++++++---- testing/events.go | 15 +++++++++++++++ testing/path.go | 19 ++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/testing/endpoint.go b/testing/endpoint.go index 4c3804879c9..39fe2b4408c 100644 --- a/testing/endpoint.go +++ b/testing/endpoint.go @@ -3,7 +3,7 @@ package ibctesting import ( "fmt" - // sdk "github.com/cosmos/cosmos-sdk/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" @@ -375,6 +375,17 @@ func (endpoint *Endpoint) SendPacket(packet exported.PacketI) error { // RecvPacket receives a packet on the associated endpoint. // The counterparty client is updated. func (endpoint *Endpoint) RecvPacket(packet channeltypes.Packet) error { + _, err := endpoint.RecvPacketWithResult(packet) + if err != nil { + return err + } + + return nil +} + +// RecvPacketWithResult receives a packet on the associated endpoint and the result +// of the transaction is returned. The counterparty client is updated. +func (endpoint *Endpoint) RecvPacketWithResult(packet channeltypes.Packet) (*sdk.Result, error) { // get proof of packet commitment on source packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) proof, proofHeight := endpoint.Counterparty.Chain.QueryProof(packetKey) @@ -382,11 +393,16 @@ func (endpoint *Endpoint) RecvPacket(packet channeltypes.Packet) error { recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String()) // receive on counterparty and update source client - if err := endpoint.Chain.sendMsgs(recvMsg); err != nil { - return err + res, err := endpoint.Chain.SendMsgs(recvMsg) + if err != nil { + return nil, err } - return endpoint.Counterparty.UpdateClient() + if err := endpoint.Counterparty.UpdateClient(); err != nil { + return nil, err + } + + return res, nil } // WriteAcknowledgement writes an acknowledgement on the channel associated with the endpoint. diff --git a/testing/events.go b/testing/events.go index 9666a82ffca..7f38b2ccd7c 100644 --- a/testing/events.go +++ b/testing/events.go @@ -55,3 +55,18 @@ func ParseChannelIDFromEvents(events sdk.Events) (string, error) { } return "", fmt.Errorf("channel identifier event attribute not found") } + +// ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the +// acknowledgement. +func ParseAckFromEvents(events sdk.Events) ([]byte, error) { + for _, ev := range events { + if ev.Type == channeltypes.EventTypeRecvPacket { + for _, attr := range ev.Attributes { + if string(attr.Key) == channeltypes.AttributeKeyAck { + return attr.Value, nil + } + } + } + } + return nil, fmt.Errorf("acknowledgement event attribute not found") +} diff --git a/testing/path.go b/testing/path.go index d90bc5e1171..f60faeaa051 100644 --- a/testing/path.go +++ b/testing/path.go @@ -38,14 +38,20 @@ func (path *Path) SetChannelOrdered() { // RelayPacket attempts to relay the packet first on EndpointA and then on EndpointB // 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, ack []byte) error { +func (path *Path) RelayPacket(packet channeltypes.Packet, _ []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)) { // packet found, relay from A to B path.EndpointB.UpdateClient() - if err := path.EndpointB.RecvPacket(packet); err != nil { + res, err := path.EndpointB.RecvPacketWithResult(packet) + if err != nil { + return err + } + + ack, err := ParseAckFromEvents(res.GetEvents()) + if err != nil { return err } @@ -62,9 +68,16 @@ func (path *Path) RelayPacket(packet channeltypes.Packet, ack []byte) error { // packet found, relay B to A path.EndpointA.UpdateClient() - if err := path.EndpointA.RecvPacket(packet); err != nil { + res, err := path.EndpointA.RecvPacketWithResult(packet) + if err != nil { return err } + + ack, err := ParseAckFromEvents(res.GetEvents()) + if err != nil { + return err + } + if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil { return err } From 7b7afcac39a7f50c80785e13ae4b1001df97722b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Fri, 28 Jan 2022 12:24:01 +0100 Subject: [PATCH 2/4] changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index acfcf52334e..e6facb23d66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (testing) [\#810](https://github.com/cosmos/ibc-go/pull/810) Additional testing function added to `Endpoint` type called `RecvPacketWithResult`. Performs the same functionality as the existing `RecvPacket` function but also returns the message result. `path.RelayPacket` no longer uses the provided acknowledgement argument and instead obtains the acknoweledgement via MsgRecvPacket events. * (connection) [\#721](https://github.com/cosmos/ibc-go/pull/721) Simplify connection handshake error messages when unpacking client state. * (channel) [\#692](https://github.com/cosmos/ibc-go/pull/692) Minimize channel logging by only emitting the packet sequence, source port/channel, destination port/channel upon packet receives, acknowledgements and timeouts. * [\#383](https://github.com/cosmos/ibc-go/pull/383) Adds helper functions for merging and splitting middleware versions from the underlying app version. From d3f5885cb192ac728e95be915ce6e4db50c1d25f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Fri, 28 Jan 2022 12:24:45 +0100 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6facb23d66..8e7ff94dc07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,7 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (testing) [\#810](https://github.com/cosmos/ibc-go/pull/810) Additional testing function added to `Endpoint` type called `RecvPacketWithResult`. Performs the same functionality as the existing `RecvPacket` function but also returns the message result. `path.RelayPacket` no longer uses the provided acknowledgement argument and instead obtains the acknoweledgement via MsgRecvPacket events. +* (testing) [\#810](https://github.com/cosmos/ibc-go/pull/810) Additional testing function added to `Endpoint` type called `RecvPacketWithResult`. Performs the same functionality as the existing `RecvPacket` function but also returns the message result. `path.RelayPacket` no longer uses the provided acknowledgement argument and instead obtains the acknowledgement via MsgRecvPacket events. * (connection) [\#721](https://github.com/cosmos/ibc-go/pull/721) Simplify connection handshake error messages when unpacking client state. * (channel) [\#692](https://github.com/cosmos/ibc-go/pull/692) Minimize channel logging by only emitting the packet sequence, source port/channel, destination port/channel upon packet receives, acknowledgements and timeouts. * [\#383](https://github.com/cosmos/ibc-go/pull/383) Adds helper functions for merging and splitting middleware versions from the underlying app version. From 299490a83787dcca8d6d710a0daa7baf1e298cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Fri, 28 Jan 2022 13:34:15 +0100 Subject: [PATCH 4/4] fix: event parsing bug ack emitted in write ack event not recv packet event --- testing/events.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/events.go b/testing/events.go index 7f38b2ccd7c..037a4c342e3 100644 --- a/testing/events.go +++ b/testing/events.go @@ -60,7 +60,7 @@ func ParseChannelIDFromEvents(events sdk.Events) (string, error) { // acknowledgement. func ParseAckFromEvents(events sdk.Events) ([]byte, error) { for _, ev := range events { - if ev.Type == channeltypes.EventTypeRecvPacket { + if ev.Type == channeltypes.EventTypeWriteAck { for _, attr := range ev.Attributes { if string(attr.Key) == channeltypes.AttributeKeyAck { return attr.Value, nil