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

feat: support timeout handling in chain provider #57

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions pkg/relay/ethereum/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,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)
Expand Down
34 changes: 34 additions & 0 deletions pkg/relay/ethereum/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ 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/accounts/abi/bind"
"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"

Expand Down Expand Up @@ -48,6 +50,38 @@ func init() {
abiChannelUpgradeOpen = abiIBCHandler.Events["ChannelUpgradeOpen"]
}

// 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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly obtain an array value by using crypto.Keccak256Hash.
Also, you can use host.PacketCommitmentKey to remove a type casting.

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} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[32]byte{0} and [32]byte{0,0} and [32]byte{0,0,0} and ... are all equivalent, but [32]byte{} is the simplest within this family.

continue
}

activePackets = append(activePackets, packet)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove trailing blank lines from both functions and blocks.

}

return activePackets, nil

}

func (chain *Chain) findSentPackets(ctx core.QueryContext, fromHeight uint64) (core.PacketInfoList, error) {
logger := chain.GetChannelLogger()
now := time.Now()
Expand Down
20 changes: 20 additions & 0 deletions pkg/relay/ethereum/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,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,
},
Comment on lines +361 to +370
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define and use pbToPacket in pkg/relay/ethereum/types.go.

Proof: msg.ProofUnreceived,
ProofHeight: pbToHandlerHeight(msg.ProofHeight),
NextSequenceRecv: msg.NextSequenceRecv,
})
}

func (c *Chain) TxChannelUpgradeInit(opts *bind.TransactOpts, msg *chantypes.MsgChannelUpgradeInit) (*gethtypes.Transaction, error) {
return c.ibcHandler.ChannelUpgradeInit(opts, ibchandler.IIBCChannelUpgradeBaseMsgChannelUpgradeInit{
PortId: c.pathEnd.PortID,
Expand Down Expand Up @@ -468,6 +486,8 @@ func (c *Chain) BuildMessageTx(opts *bind.TransactOpts, msg sdk.Msg, skipUpdateC
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 *chantypes.MsgChannelUpgradeInit:
tx, err = c.TxChannelUpgradeInit(opts, msg)
case *chantypes.MsgChannelUpgradeTry:
Expand Down