Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: add function RelayPacketWithResults #3986

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 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 @@ -83,3 +85,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) {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
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
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
}

return res, ack, path.EndpointA.AcknowledgePacket(packet, ack)
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
}

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
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
}

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

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