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

refactor state based relaying (fixes update client bug on retries) #435

Merged
merged 15 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
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
195 changes: 195 additions & 0 deletions relayer/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package relayer

import (
"fmt"

retry "github.com/avast/retry-go"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
Expand Down Expand Up @@ -320,3 +323,195 @@ func (c *Chain) MsgTransfer(dst *PathEnd, amount sdk.Coin, dstAddr string,
timeoutTimestamp,
)
}

// MsgRelayTimeout
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
func (c *Chain) MsgRelayTimeout(counterparty *Chain, packet *relayMsgTimeout) (msgs []sdk.Msg, err error) {
var recvRes *chantypes.QueryPacketReceiptResponse
// TODO: try commenting out retries to reduce complexity
// retry getting commit response until it succeeds
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
if err = retry.Do(func() error {
// NOTE: Timeouts currently only work with ORDERED channels for nwo
// NOTE: the proof height uses - 1 due to tendermint's delayed execution model
recvRes, err = counterparty.QueryPacketReceipt(int64(counterparty.MustGetLatestLightHeight())-1, packet.seq)
if err != nil {
return err
}

if recvRes.Proof == nil {
return fmt.Errorf("timeout packet receipt proof seq(%d) is nil", packet.seq)
}

return nil
}, rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
// clear messages
msgs = []sdk.Msg{}

// OnRetry we want to update the light clients and then debug log
updateMsg, err := c.UpdateClient(counterparty)
if err != nil {
return
}

msgs = append(msgs, updateMsg)

if counterparty.debug {
counterparty.Log(fmt.Sprintf("- [%s]@{%d} - try(%d/%d) query packet receipt: %s",
counterparty.ChainID, counterparty.MustGetLatestLightHeight()-1, n+1, rtyAttNum, err))
}

})); err != nil {
counterparty.Error(err)
return
}

if recvRes == nil {
return nil, fmt.Errorf("timeout packet [%s]seq{%d} has no associated proofs", c.ChainID, packet.seq)
}

version := clienttypes.ParseChainID(counterparty.ChainID)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
msg := chantypes.NewMsgTimeout(
chantypes.NewPacket(
packet.packetData,
packet.seq,
c.PathEnd.PortID,
c.PathEnd.ChannelID,
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
clienttypes.NewHeight(version, packet.timeout),
packet.timeoutStamp,
),
packet.seq,
recvRes.Proof,
recvRes.ProofHeight,
c.MustGetAddress(),
)

return append(msgs, msg), nil
}

// MsgRelayRecvPacket
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
func (c *Chain) MsgRelayRecvPacket(counterparty *Chain, packet *relayMsgRecvPacket) (msgs []sdk.Msg, err error) {
var comRes *chantypes.QueryPacketCommitmentResponse
// TODO: try commenting out retries to reduce complexity
// retry getting commit response until it succeeds
if err = retry.Do(func() error {
// NOTE: the proof height uses - 1 due to tendermint's delayed execution model
comRes, err = counterparty.QueryPacketCommitment(int64(counterparty.MustGetLatestLightHeight())-1, packet.seq)
if err != nil {
return err
}

if comRes.Proof == nil || comRes.Commitment == nil {
return fmt.Errorf("recv packet commitment query seq(%d) is nil", packet.seq)
}

return nil
}, rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
// clear messages
msgs = []sdk.Msg{}

// OnRetry we want to update the light clients and then debug log
updateMsg, err := c.UpdateClient(counterparty)
if err != nil {
return
}

msgs = append(msgs, updateMsg)

if counterparty.debug {
counterparty.Log(fmt.Sprintf("- [%s]@{%d} - try(%d/%d) query packet commitment: %s",
counterparty.ChainID, counterparty.MustGetLatestLightHeight()-1, n+1, rtyAttNum, err))
}

})); err != nil {
counterparty.Error(err)
return
}

if comRes == nil {
return nil, fmt.Errorf("receive packet [%s]seq{%d} has no associated proofs", c.ChainID, packet.seq)
}

version := clienttypes.ParseChainID(counterparty.ChainID)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
msg := chantypes.NewMsgRecvPacket(
chantypes.NewPacket(
packet.packetData,
packet.seq,
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
c.PathEnd.PortID,
c.PathEnd.ChannelID,
clienttypes.NewHeight(version, packet.timeout),
packet.timeoutStamp,
),
comRes.Proof,
comRes.ProofHeight,
c.MustGetAddress(),
)

return append(msgs, msg), nil
}

// MsgRelayAcknowledgement
func (c *Chain) MsgRelayAcknowledgement(counterparty *Chain, packet *relayMsgPacketAck) (msgs []sdk.Msg, err error) {
var ackRes *chantypes.QueryPacketAcknowledgementResponse
// TODO: try commenting out retries to reduce complexity
// retry getting commit response until it succeeds
if err = retry.Do(func() error {
// NOTE: the proof height uses - 1 due to tendermint's delayed execution model
ackRes, err = counterparty.QueryPacketAcknowledgement(int64(counterparty.MustGetLatestLightHeight())-1, packet.seq)
if err != nil {
return err
}

if ackRes.Proof == nil || ackRes.Acknowledgement == nil {
return fmt.Errorf("ack packet acknowledgement query seq(%d) is nil", packet.seq)
}

return nil
}, rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
// clear messages
msgs = []sdk.Msg{}

// OnRetry we want to update the light clients and then debug log
updateMsg, err := c.UpdateClient(counterparty)
if err != nil {
return
}

msgs = append(msgs, updateMsg)

if counterparty.debug {
counterparty.Log(fmt.Sprintf("- [%s]@{%d} - try(%d/%d) query packet acknowledgement: %s",
counterparty.ChainID, counterparty.MustGetLatestLightHeight()-1, n+1, rtyAttNum, err))
}

})); err != nil {
counterparty.Error(err)
return
}

if ackRes == nil {
return nil, fmt.Errorf("ack packet [%s]seq{%d} has no associated proofs", counterparty.ChainID, packet.seq)
}

version := clienttypes.ParseChainID(c.ChainID)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
msg := chantypes.NewMsgAcknowledgement(
chantypes.NewPacket(
packet.packetData,
packet.seq,
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
c.PathEnd.PortID,
c.PathEnd.ChannelID,
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
clienttypes.NewHeight(version, packet.timeout),
packet.timeoutStamp,
),
packet.ack,
ackRes.Proof,
ackRes.ProofHeight,
counterparty.MustGetAddress(),
)

return append(msgs, msg), nil
}
Loading