From 22209bc91062cabb5b68958f53676dfeb0e73719 Mon Sep 17 00:00:00 2001 From: Abolfazl Soltani Date: Fri, 21 Jul 2023 15:43:39 +0800 Subject: [PATCH 1/2] imp: adding `IsOpen` and `IsClosed` methods to Channel type (#4152) --- .../controller/keeper/handshake.go | 2 +- .../27-interchain-accounts/controller/keeper/keeper.go | 4 ++-- .../27-interchain-accounts/host/keeper/handshake.go | 2 +- .../apps/27-interchain-accounts/host/keeper/keeper.go | 3 +-- modules/core/04-channel/types/channel.go | 10 ++++++++++ 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/modules/apps/27-interchain-accounts/controller/keeper/handshake.go b/modules/apps/27-interchain-accounts/controller/keeper/handshake.go index 1bc75623f71..8084c8e5d66 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/handshake.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/handshake.go @@ -67,7 +67,7 @@ func (k Keeper) OnChanOpenInit( panic(fmt.Sprintf("active channel mapping set for %s but channel does not exist in channel store", activeChannelID)) } - if channel.State == channeltypes.OPEN { + if channel.IsOpen() { return "", errorsmod.Wrapf(icatypes.ErrActiveChannelAlreadySet, "existing active channel %s for portID %s is already OPEN", activeChannelID, portID) } diff --git a/modules/apps/27-interchain-accounts/controller/keeper/keeper.go b/modules/apps/27-interchain-accounts/controller/keeper/keeper.go index eee552337d8..20d83a01ab8 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/keeper.go @@ -146,7 +146,7 @@ func (k Keeper) GetOpenActiveChannel(ctx sdk.Context, connectionID, portID strin channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID) - if found && channel.State == channeltypes.OPEN { + if found && channel.IsOpen() { return channelID, true } @@ -161,7 +161,7 @@ func (k Keeper) IsActiveChannelClosed(ctx sdk.Context, connectionID, portID stri } channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID) - return found && channel.State == channeltypes.CLOSED + return found && channel.IsClosed() } // GetAllActiveChannels returns a list of all active interchain accounts controller channels and their associated connection and port identifiers diff --git a/modules/apps/27-interchain-accounts/host/keeper/handshake.go b/modules/apps/27-interchain-accounts/host/keeper/handshake.go index 8d43071ac2a..f565ad65a7d 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/handshake.go +++ b/modules/apps/27-interchain-accounts/host/keeper/handshake.go @@ -51,7 +51,7 @@ func (k Keeper) OnChanOpenTry( panic(fmt.Sprintf("active channel mapping set for %s but channel does not exist in channel store", activeChannelID)) } - if channel.State == channeltypes.OPEN { + if channel.IsOpen() { return "", errorsmod.Wrapf(icatypes.ErrActiveChannelAlreadySet, "existing active channel %s for portID %s is already OPEN", activeChannelID, portID) } diff --git a/modules/apps/27-interchain-accounts/host/keeper/keeper.go b/modules/apps/27-interchain-accounts/host/keeper/keeper.go index 1de40965a2f..40698005c46 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/host/keeper/keeper.go @@ -17,7 +17,6 @@ import ( genesistypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/genesis/types" "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" host "github.com/cosmos/ibc-go/v7/modules/core/24-host" ibcerrors "github.com/cosmos/ibc-go/v7/modules/core/errors" @@ -146,7 +145,7 @@ func (k Keeper) GetOpenActiveChannel(ctx sdk.Context, connectionID, portID strin channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID) - if found && channel.State == channeltypes.OPEN { + if found && channel.IsOpen() { return channelID, true } diff --git a/modules/core/04-channel/types/channel.go b/modules/core/04-channel/types/channel.go index 012b571fb07..1ba7b78c4f6 100644 --- a/modules/core/04-channel/types/channel.go +++ b/modules/core/04-channel/types/channel.go @@ -51,6 +51,16 @@ func (ch Channel) GetVersion() string { return ch.Version } +// IsOpen returns true if the channel state is OPEN +func (ch Channel) IsOpen() bool { + return ch.State == OPEN +} + +// IsClosed returns true if the channel state is CLOSED +func (ch Channel) IsClosed() bool { + return ch.State == CLOSED +} + // ValidateBasic performs a basic validation of the channel fields func (ch Channel) ValidateBasic() error { if ch.State == UNINITIALIZED { From 13ac6975772f934a4a22979cb586d0cc933d8c02 Mon Sep 17 00:00:00 2001 From: Abolfazl Soltani Date: Fri, 21 Jul 2023 17:53:44 +0800 Subject: [PATCH 2/2] imp: use `IsClosed` method of channel anywhere (#4152) --- modules/core/04-channel/keeper/handshake.go | 4 ++-- modules/core/04-channel/keeper/timeout.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/04-channel/keeper/handshake.go b/modules/core/04-channel/keeper/handshake.go index 92241432503..61ab05407c1 100644 --- a/modules/core/04-channel/keeper/handshake.go +++ b/modules/core/04-channel/keeper/handshake.go @@ -385,7 +385,7 @@ func (k Keeper) ChanCloseInit( return errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID) } - if channel.State == types.CLOSED { + if channel.IsClosed() { return errorsmod.Wrap(types.ErrInvalidChannelState, "channel is already CLOSED") } @@ -441,7 +441,7 @@ func (k Keeper) ChanCloseConfirm( return errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID) } - if channel.State == types.CLOSED { + if channel.IsClosed() { return errorsmod.Wrap(types.ErrInvalidChannelState, "channel is already CLOSED") } diff --git a/modules/core/04-channel/keeper/timeout.go b/modules/core/04-channel/keeper/timeout.go index c0248917306..a596c3d7558 100644 --- a/modules/core/04-channel/keeper/timeout.go +++ b/modules/core/04-channel/keeper/timeout.go @@ -171,7 +171,7 @@ func (k Keeper) TimeoutExecuted( // emit an event marking that we have processed the timeout emitTimeoutPacketEvent(ctx, packet, channel) - if channel.Ordering == types.ORDERED && channel.State == types.CLOSED { + if channel.Ordering == types.ORDERED && channel.IsClosed() { emitChannelClosedEvent(ctx, packet, channel) }