From 5221485ae2049923c2c1e07f147ddba9de20659a Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Thu, 20 Jul 2023 11:05:23 +0300 Subject: [PATCH] imp: use HasInflightPackets to check if channel has finished flushing. --- modules/core/04-channel/keeper/keeper.go | 4 ++-- modules/core/04-channel/keeper/packet.go | 2 +- modules/core/04-channel/keeper/timeout.go | 2 +- modules/core/04-channel/keeper/upgrade.go | 6 +++--- modules/core/keeper/msg_server.go | 14 ++++---------- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/modules/core/04-channel/keeper/keeper.go b/modules/core/04-channel/keeper/keeper.go index 399f6c7ac30..7d68521488e 100644 --- a/modules/core/04-channel/keeper/keeper.go +++ b/modules/core/04-channel/keeper/keeper.go @@ -571,11 +571,11 @@ func (k Keeper) iterateHashes(ctx sdk.Context, iterator db.Iterator, cb func(por } } -// hasInflightPackets returns true if there are packet commitments stored at the specified +// HasInflightPackets returns true if there are packet commitments stored at the specified // port and channel, and false otherwise. // //lint:ignore U1000 Ignore unused function temporarily for debugging -func (k Keeper) hasInflightPackets(ctx sdk.Context, portID, channelID string) bool { +func (k Keeper) HasInflightPackets(ctx sdk.Context, portID, channelID string) bool { iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), []byte(host.PacketCommitmentPrefixPath(portID, channelID))) defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) diff --git a/modules/core/04-channel/keeper/packet.go b/modules/core/04-channel/keeper/packet.go index 3c5a06d017c..3cc165d5d51 100644 --- a/modules/core/04-channel/keeper/packet.go +++ b/modules/core/04-channel/keeper/packet.go @@ -477,7 +477,7 @@ func (k Keeper) AcknowledgePacket( // Delete packet commitment, since the packet has been acknowledged, the commitement is no longer necessary k.deletePacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - if channel.FlushStatus == types.FLUSHING && !k.hasInflightPackets(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) { + if channel.FlushStatus == types.FLUSHING && !k.HasInflightPackets(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) { channel.FlushStatus = types.FLUSHCOMPLETE k.SetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), channel) } diff --git a/modules/core/04-channel/keeper/timeout.go b/modules/core/04-channel/keeper/timeout.go index 2cf23767c73..7c3883f513f 100644 --- a/modules/core/04-channel/keeper/timeout.go +++ b/modules/core/04-channel/keeper/timeout.go @@ -153,7 +153,7 @@ func (k Keeper) TimeoutExecuted( k.deletePacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - if channel.FlushStatus == types.FLUSHING && !k.hasInflightPackets(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) { + if channel.FlushStatus == types.FLUSHING && !k.HasInflightPackets(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) { channel.FlushStatus = types.FLUSHCOMPLETE k.SetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), channel) } diff --git a/modules/core/04-channel/keeper/upgrade.go b/modules/core/04-channel/keeper/upgrade.go index 6aaf37b55df..f7ebeb000cb 100644 --- a/modules/core/04-channel/keeper/upgrade.go +++ b/modules/core/04-channel/keeper/upgrade.go @@ -193,7 +193,7 @@ func (k Keeper) WriteUpgradeTryChannel(ctx sdk.Context, portID, channelID string channel.State = types.TRYUPGRADE channel.FlushStatus = types.FLUSHING - if !k.hasInflightPackets(ctx, portID, channelID) { + if !k.HasInflightPackets(ctx, portID, channelID) { channel.FlushStatus = types.FLUSHCOMPLETE } @@ -291,7 +291,7 @@ func (k Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID, upgra channel.State = types.ACKUPGRADE channel.FlushStatus = types.FLUSHING - if !k.hasInflightPackets(ctx, portID, channelID) { + if !k.HasInflightPackets(ctx, portID, channelID) { channel.FlushStatus = types.FLUSHCOMPLETE } @@ -322,7 +322,7 @@ func (k Keeper) ChanUpgradeOpen( proofCounterpartyChannel []byte, proofHeight clienttypes.Height, ) error { - if k.hasInflightPackets(ctx, portID, channelID) { + if k.HasInflightPackets(ctx, portID, channelID) { return errorsmod.Wrapf(types.ErrPendingInflightPackets, "port ID (%s) channel ID (%s)", portID, channelID) } diff --git a/modules/core/keeper/msg_server.go b/modules/core/keeper/msg_server.go index ef1d6b3b43c..aa02af43ce5 100644 --- a/modules/core/keeper/msg_server.go +++ b/modules/core/keeper/msg_server.go @@ -854,18 +854,12 @@ func (k Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgCh // Move channel to OPEN state if both chains have finished flushing any in-flight packets. Counterparty flush status // has been verified in ChanUpgradeAck. - if msg.CounterpartyFlushStatus == channeltypes.FLUSHCOMPLETE { - channel, found := k.ChannelKeeper.GetChannel(ctx, msg.PortId, msg.ChannelId) - if !found { - return nil, errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) - } - if channel.FlushStatus == channeltypes.FLUSHCOMPLETE { - cbs.OnChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId) + if msg.CounterpartyFlushStatus == channeltypes.FLUSHCOMPLETE && !k.ChannelKeeper.HasInflightPackets(ctx, msg.PortId, msg.ChannelId) { + cbs.OnChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId) - k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId) + k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId) - ctx.Logger().Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) - } + ctx.Logger().Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) } return &channeltypes.MsgChannelUpgradeAckResponse{Result: channeltypes.SUCCESS}, nil