Skip to content

Commit

Permalink
add helper function in testing package: RecvPacketWithResult (#810)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
colin-axner authored Jan 28, 2022
1 parent 3c2f2eb commit 54dc848
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.
Expand Down
24 changes: 20 additions & 4 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -375,18 +375,34 @@ 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)

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.
Expand Down
15 changes: 15 additions & 0 deletions testing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.EventTypeWriteAck {
for _, attr := range ev.Attributes {
if string(attr.Key) == channeltypes.AttributeKeyAck {
return attr.Value, nil
}
}
}
}
return nil, fmt.Errorf("acknowledgement event attribute not found")
}
19 changes: 16 additions & 3 deletions testing/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit 54dc848

Please sign in to comment.