Skip to content

Commit

Permalink
testing: add function RelayPacketWithResults (cosmos#3986)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Rodriguez authored and faddat committed Jul 5, 2023
1 parent e1a622d commit 1c07a6b
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions testing/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
)

Expand Down Expand Up @@ -39,47 +41,65 @@ 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 {
_, _, err := path.RelayPacketWithResults(packet)
return err
}

// 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) (*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)) {

// packet found, relay from A to B
if err := path.EndpointB.UpdateClient(); err != nil {
return err
return nil, nil, err
}

res, err := path.EndpointB.RecvPacketWithResult(packet)
if err != nil {
return err
return nil, nil, err
}

ack, err := ParseAckFromEvents(res.Events)
if err != nil {
return err
return nil, nil, err
}

return path.EndpointA.AcknowledgePacket(packet, ack)
if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil {
return nil, nil, err
}

return res, ack, nil
}

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
return nil, nil, err
}

res, err := path.EndpointA.RecvPacketWithResult(packet)
if err != nil {
return err
return nil, nil, err
}

ack, err := ParseAckFromEvents(res.Events)
if err != nil {
return err
return nil, nil, err
}

if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
return nil, nil, err
}

return path.EndpointB.AcknowledgePacket(packet, ack)
return res, ack, nil
}

return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
return nil, nil, fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
}

0 comments on commit 1c07a6b

Please sign in to comment.