From 526e444157ac0d4142aa7d2609e61115683387ae Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Wed, 16 Oct 2024 16:56:06 +0200 Subject: [PATCH] feat: support timeout handling --- pkg/relay/ethereum/chain.go | 6 ++++++ pkg/relay/ethereum/events.go | 34 ++++++++++++++++++++++++++++++++++ pkg/relay/ethereum/tx.go | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/pkg/relay/ethereum/chain.go b/pkg/relay/ethereum/chain.go index cfb8ecc..9f9b524 100644 --- a/pkg/relay/ethereum/chain.go +++ b/pkg/relay/ethereum/chain.go @@ -356,6 +356,12 @@ func (c *Chain) QueryUnfinalizedRelayPackets(ctx core.QueryContext, counterparty return nil, err } + packets, err = c.filterPacketsWithActiveCommitment(ctx, packets) + if err != nil { + logger.Error("failed to filter packets with active commitment", err) + return nil, err + } + counterpartyHeader, err := counterparty.GetLatestFinalizedHeader() if err != nil { logger.Error("failed to get latest finalized header", err) diff --git a/pkg/relay/ethereum/events.go b/pkg/relay/ethereum/events.go index a048230..e818de4 100644 --- a/pkg/relay/ethereum/events.go +++ b/pkg/relay/ethereum/events.go @@ -7,10 +7,12 @@ import ( clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v8/modules/core/24-host" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/hyperledger-labs/yui-relayer/core" "github.com/hyperledger-labs/yui-relayer/log" @@ -46,6 +48,38 @@ func init() { } +// filterPacketsWithActiveCommitment filters packets with non-zero packet commitments +func (chain *Chain) filterPacketsWithActiveCommitment(ctx core.QueryContext, packets core.PacketInfoList) (core.PacketInfoList, error) { + + var activePackets core.PacketInfoList + + for _, packet := range packets { + commitmentPath := host.PacketCommitmentPath(packet.SourcePort, packet.SourceChannel, packet.Sequence) + commitmentKey := crypto.Keccak256([]byte(commitmentPath)) + var fixedArray [32]byte + copy(fixedArray[:], commitmentKey) // Copy the slice into the fixed array + + res, err := chain.ibcHandler.GetCommitment( + chain.callOptsFromQueryContext(ctx), + fixedArray, + ) + + if err != nil { + return packets, err + } + + if res == [32]byte{0} { + continue + } + + activePackets = append(activePackets, packet) + + } + + return activePackets, nil + +} + func (chain *Chain) findSentPackets(ctx core.QueryContext, fromHeight uint64) (core.PacketInfoList, error) { logger := chain.GetChannelLogger() now := time.Now() diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 9d96c5b..9d259fd 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -378,6 +378,24 @@ func (c *Chain) TxAcknowledgement(opts *bind.TransactOpts, msg *chantypes.MsgAck }) } +func (c *Chain) TxMsgTimeout(opts *bind.TransactOpts, msg *chantypes.MsgTimeout) (*gethtypes.Transaction, error) { + return c.ibcHandler.TimeoutPacket(opts, ibchandler.IIBCChannelPacketTimeoutMsgTimeoutPacket{ + Packet: ibchandler.Packet{ + Sequence: msg.Packet.Sequence, + SourcePort: msg.Packet.SourcePort, + SourceChannel: msg.Packet.SourceChannel, + DestinationPort: msg.Packet.DestinationPort, + DestinationChannel: msg.Packet.DestinationChannel, + Data: msg.Packet.Data, + TimeoutHeight: ibchandler.HeightData(msg.Packet.TimeoutHeight), + TimeoutTimestamp: msg.Packet.TimeoutTimestamp, + }, + Proof: msg.ProofUnreceived, + ProofHeight: pbToHandlerHeight(msg.ProofHeight), + NextSequenceRecv: msg.NextSequenceRecv, + }) +} + func (c *Chain) SendTx(opts *bind.TransactOpts, msg sdk.Msg, skipUpdateClientCommitment bool) (*gethtypes.Transaction, error) { logger := c.GetChainLogger() var ( @@ -409,6 +427,8 @@ func (c *Chain) SendTx(opts *bind.TransactOpts, msg sdk.Msg, skipUpdateClientCom tx, err = c.TxRecvPacket(opts, msg) case *chantypes.MsgAcknowledgement: tx, err = c.TxAcknowledgement(opts, msg) + case *chantypes.MsgTimeout: + tx, err = c.TxMsgTimeout(opts, msg) // case *transfertypes.MsgTransfer: // err = c.client.transfer(msg) default: