Skip to content

Commit 54dc848

Browse files
authoredJan 28, 2022
add helper function in testing package: RecvPacketWithResult (#810)
## Description Allows the acknowledgement to be parsed from MsgRecvPacket results. Adjusts RelayPacket to use the acknowledgement from the events rather than expecting the acknowledgement to be passed in before hand. Useful for #701 Will make a followup pr breaking RelayPacket API closes: #XXXX --- Before we can merge this PR, please make sure that all the following items have been checked off. If any of the checklist items are not applicable, please leave them but write a little note why. - [ ] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md). - [ ] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing) - [ ] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`) - [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [ ] Re-reviewed `Files changed` in the Github PR explorer - [ ] Review `Codecov Report` in the comment section below once CI passes
1 parent 3c2f2eb commit 54dc848

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5757

5858
### Improvements
5959

60+
* (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.
6061
* (connection) [\#721](https://github.com/cosmos/ibc-go/pull/721) Simplify connection handshake error messages when unpacking client state.
6162
* (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.
6263
* [\#383](https://github.com/cosmos/ibc-go/pull/383) Adds helper functions for merging and splitting middleware versions from the underlying app version.

‎testing/endpoint.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ibctesting
33
import (
44
"fmt"
55

6-
// sdk "github.com/cosmos/cosmos-sdk/types"
6+
sdk "github.com/cosmos/cosmos-sdk/types"
77
"github.com/stretchr/testify/require"
88

99
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
@@ -375,18 +375,34 @@ func (endpoint *Endpoint) SendPacket(packet exported.PacketI) error {
375375
// RecvPacket receives a packet on the associated endpoint.
376376
// The counterparty client is updated.
377377
func (endpoint *Endpoint) RecvPacket(packet channeltypes.Packet) error {
378+
_, err := endpoint.RecvPacketWithResult(packet)
379+
if err != nil {
380+
return err
381+
}
382+
383+
return nil
384+
}
385+
386+
// RecvPacketWithResult receives a packet on the associated endpoint and the result
387+
// of the transaction is returned. The counterparty client is updated.
388+
func (endpoint *Endpoint) RecvPacketWithResult(packet channeltypes.Packet) (*sdk.Result, error) {
378389
// get proof of packet commitment on source
379390
packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
380391
proof, proofHeight := endpoint.Counterparty.Chain.QueryProof(packetKey)
381392

382393
recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String())
383394

384395
// receive on counterparty and update source client
385-
if err := endpoint.Chain.sendMsgs(recvMsg); err != nil {
386-
return err
396+
res, err := endpoint.Chain.SendMsgs(recvMsg)
397+
if err != nil {
398+
return nil, err
387399
}
388400

389-
return endpoint.Counterparty.UpdateClient()
401+
if err := endpoint.Counterparty.UpdateClient(); err != nil {
402+
return nil, err
403+
}
404+
405+
return res, nil
390406
}
391407

392408
// WriteAcknowledgement writes an acknowledgement on the channel associated with the endpoint.

‎testing/events.go

+15
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,18 @@ func ParseChannelIDFromEvents(events sdk.Events) (string, error) {
5555
}
5656
return "", fmt.Errorf("channel identifier event attribute not found")
5757
}
58+
59+
// ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the
60+
// acknowledgement.
61+
func ParseAckFromEvents(events sdk.Events) ([]byte, error) {
62+
for _, ev := range events {
63+
if ev.Type == channeltypes.EventTypeWriteAck {
64+
for _, attr := range ev.Attributes {
65+
if string(attr.Key) == channeltypes.AttributeKeyAck {
66+
return attr.Value, nil
67+
}
68+
}
69+
}
70+
}
71+
return nil, fmt.Errorf("acknowledgement event attribute not found")
72+
}

‎testing/path.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@ func (path *Path) SetChannelOrdered() {
3838
// RelayPacket attempts to relay the packet first on EndpointA and then on EndpointB
3939
// if EndpointA does not contain a packet commitment for that packet. An error is returned
4040
// if a relay step fails or the packet commitment does not exist on either endpoint.
41-
func (path *Path) RelayPacket(packet channeltypes.Packet, ack []byte) error {
41+
func (path *Path) RelayPacket(packet channeltypes.Packet, _ []byte) error {
4242
pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
4343
if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App.AppCodec(), packet)) {
4444

4545
// packet found, relay from A to B
4646
path.EndpointB.UpdateClient()
4747

48-
if err := path.EndpointB.RecvPacket(packet); err != nil {
48+
res, err := path.EndpointB.RecvPacketWithResult(packet)
49+
if err != nil {
50+
return err
51+
}
52+
53+
ack, err := ParseAckFromEvents(res.GetEvents())
54+
if err != nil {
4955
return err
5056
}
5157

@@ -62,9 +68,16 @@ func (path *Path) RelayPacket(packet channeltypes.Packet, ack []byte) error {
6268
// packet found, relay B to A
6369
path.EndpointA.UpdateClient()
6470

65-
if err := path.EndpointA.RecvPacket(packet); err != nil {
71+
res, err := path.EndpointA.RecvPacketWithResult(packet)
72+
if err != nil {
6673
return err
6774
}
75+
76+
ack, err := ParseAckFromEvents(res.GetEvents())
77+
if err != nil {
78+
return err
79+
}
80+
6881
if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
6982
return err
7083
}

0 commit comments

Comments
 (0)