diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bc374457be..4243f1abca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (core) [\#3082](https://github.com/cosmos/ibc-go/pull/3082) Add `HasConnection` and `HasChannel` methods. + ### Features * [\#3079](https://github.com/cosmos/ibc-go/pull/3079) Add authz support for ics20. diff --git a/modules/apps/29-fee/keeper/keeper.go b/modules/apps/29-fee/keeper/keeper.go index e506ef8118f..5d0ef4b3811 100644 --- a/modules/apps/29-fee/keeper/keeper.go +++ b/modules/apps/29-fee/keeper/keeper.go @@ -291,7 +291,7 @@ func (k Keeper) GetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) store := ctx.KVStore(k.storeKey) key := types.KeyFeesInEscrow(packetID) bz := store.Get(key) - if bz == nil { + if len(bz) == 0 { return types.PacketFees{}, false } diff --git a/modules/apps/transfer/keeper/keeper.go b/modules/apps/transfer/keeper/keeper.go index 9688d57aff2..4d57d95b28b 100644 --- a/modules/apps/transfer/keeper/keeper.go +++ b/modules/apps/transfer/keeper/keeper.go @@ -93,7 +93,7 @@ func (k Keeper) SetPort(ctx sdk.Context, portID string) { func (k Keeper) GetDenomTrace(ctx sdk.Context, denomTraceHash tmbytes.HexBytes) (types.DenomTrace, bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DenomTraceKey) bz := store.Get(denomTraceHash) - if bz == nil { + if len(bz) == 0 { return types.DenomTrace{}, false } diff --git a/modules/core/02-client/keeper/keeper.go b/modules/core/02-client/keeper/keeper.go index 634c70494fc..fba24dd5147 100644 --- a/modules/core/02-client/keeper/keeper.go +++ b/modules/core/02-client/keeper/keeper.go @@ -67,7 +67,7 @@ func (k Keeper) GenerateClientIdentifier(ctx sdk.Context, clientType string) str func (k Keeper) GetClientState(ctx sdk.Context, clientID string) (exported.ClientState, bool) { store := k.ClientStore(ctx, clientID) bz := store.Get(host.ClientStateKey()) - if bz == nil { + if len(bz) == 0 { return nil, false } @@ -85,7 +85,7 @@ func (k Keeper) SetClientState(ctx sdk.Context, clientID string, clientState exp func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height exported.Height) (exported.ConsensusState, bool) { store := k.ClientStore(ctx, clientID) bz := store.Get(host.ConsensusStateKey(height)) - if bz == nil { + if len(bz) == 0 { return nil, false } @@ -104,7 +104,7 @@ func (k Keeper) SetClientConsensusState(ctx sdk.Context, clientID string, height func (k Keeper) GetNextClientSequence(ctx sdk.Context) uint64 { store := ctx.KVStore(k.storeKey) bz := store.Get([]byte(types.KeyNextClientSequence)) - if bz == nil { + if len(bz) == 0 { panic("next client sequence is nil") } diff --git a/modules/core/03-connection/keeper/keeper.go b/modules/core/03-connection/keeper/keeper.go index 6620673ab24..ddff53f5224 100644 --- a/modules/core/03-connection/keeper/keeper.go +++ b/modules/core/03-connection/keeper/keeper.go @@ -66,7 +66,7 @@ func (k Keeper) GenerateConnectionIdentifier(ctx sdk.Context) string { func (k Keeper) GetConnection(ctx sdk.Context, connectionID string) (types.ConnectionEnd, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.ConnectionKey(connectionID)) - if bz == nil { + if len(bz) == 0 { return types.ConnectionEnd{}, false } @@ -76,6 +76,13 @@ func (k Keeper) GetConnection(ctx sdk.Context, connectionID string) (types.Conne return connection, true } +// HasConnection returns a true if the connection with the given identifier +// exists in the store. +func (k Keeper) HasConnection(ctx sdk.Context, connectionID string) bool { + store := ctx.KVStore(k.storeKey) + return store.Has(host.ConnectionKey(connectionID)) +} + // SetConnection sets a connection to the store func (k Keeper) SetConnection(ctx sdk.Context, connectionID string, connection types.ConnectionEnd) { store := ctx.KVStore(k.storeKey) @@ -105,7 +112,7 @@ func (k Keeper) GetTimestampAtHeight(ctx sdk.Context, connection types.Connectio func (k Keeper) GetClientConnectionPaths(ctx sdk.Context, clientID string) ([]string, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.ClientConnectionsKey(clientID)) - if bz == nil { + if len(bz) == 0 { return nil, false } @@ -126,7 +133,7 @@ func (k Keeper) SetClientConnectionPaths(ctx sdk.Context, clientID string, paths func (k Keeper) GetNextConnectionSequence(ctx sdk.Context) uint64 { store := ctx.KVStore(k.storeKey) bz := store.Get([]byte(types.KeyNextConnectionSequence)) - if bz == nil { + if len(bz) == 0 { panic("next connection sequence is nil") } diff --git a/modules/core/04-channel/keeper/keeper.go b/modules/core/04-channel/keeper/keeper.go index db05a8457a5..a6d5c4bf967 100644 --- a/modules/core/04-channel/keeper/keeper.go +++ b/modules/core/04-channel/keeper/keeper.go @@ -66,11 +66,17 @@ func (k Keeper) GenerateChannelIdentifier(ctx sdk.Context) string { return channelID } +// HasChannel true if the channel with the given identifiers exists in state. +func (k Keeper) HasChannel(ctx sdk.Context, portID, channelID string) bool { + store := ctx.KVStore(k.storeKey) + return store.Has(host.ChannelKey(portID, channelID)) +} + // GetChannel returns a channel with a particular identifier binded to a specific port func (k Keeper) GetChannel(ctx sdk.Context, portID, channelID string) (types.Channel, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.ChannelKey(portID, channelID)) - if bz == nil { + if len(bz) == 0 { return types.Channel{}, false } @@ -100,7 +106,7 @@ func (k Keeper) GetAppVersion(ctx sdk.Context, portID, channelID string) (string func (k Keeper) GetNextChannelSequence(ctx sdk.Context) uint64 { store := ctx.KVStore(k.storeKey) bz := store.Get([]byte(types.KeyNextChannelSequence)) - if bz == nil { + if len(bz) == 0 { panic("next channel sequence is nil") } @@ -118,7 +124,7 @@ func (k Keeper) SetNextChannelSequence(ctx sdk.Context, sequence uint64) { func (k Keeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.NextSequenceSendKey(portID, channelID)) - if bz == nil { + if len(bz) == 0 { return 0, false } @@ -136,7 +142,7 @@ func (k Keeper) SetNextSequenceSend(ctx sdk.Context, portID, channelID string, s func (k Keeper) GetNextSequenceRecv(ctx sdk.Context, portID, channelID string) (uint64, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.NextSequenceRecvKey(portID, channelID)) - if bz == nil { + if len(bz) == 0 { return 0, false } @@ -154,7 +160,7 @@ func (k Keeper) SetNextSequenceRecv(ctx sdk.Context, portID, channelID string, s func (k Keeper) GetNextSequenceAck(ctx sdk.Context, portID, channelID string) (uint64, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.NextSequenceAckKey(portID, channelID)) - if bz == nil { + if len(bz) == 0 { return 0, false } @@ -172,7 +178,7 @@ func (k Keeper) SetNextSequenceAck(ctx sdk.Context, portID, channelID string, se func (k Keeper) GetPacketReceipt(ctx sdk.Context, portID, channelID string, sequence uint64) (string, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.PacketReceiptKey(portID, channelID, sequence)) - if bz == nil { + if len(bz) == 0 { return "", false } @@ -219,7 +225,7 @@ func (k Keeper) SetPacketAcknowledgement(ctx sdk.Context, portID, channelID stri func (k Keeper) GetPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64) ([]byte, bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(host.PacketAcknowledgementKey(portID, channelID, sequence)) - if bz == nil { + if len(bz) == 0 { return nil, false } return bz, true diff --git a/modules/core/04-channel/keeper/keeper_test.go b/modules/core/04-channel/keeper/keeper_test.go index 953f5067844..c8d00843e6f 100644 --- a/modules/core/04-channel/keeper/keeper_test.go +++ b/modules/core/04-channel/keeper/keeper_test.go @@ -46,7 +46,7 @@ func (suite *KeeperTestSuite) TestSetChannel() { suite.coordinator.SetupConnections(path) // check for channel to be created on chainA - _, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.False(found) path.SetChannelOrdered() diff --git a/modules/light-clients/07-tendermint/types/store.go b/modules/light-clients/07-tendermint/types/store.go index 2a8eacee2e6..e7c79e49cb8 100644 --- a/modules/light-clients/07-tendermint/types/store.go +++ b/modules/light-clients/07-tendermint/types/store.go @@ -54,7 +54,7 @@ func SetConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensus // store. An error is returned if the consensus state does not exist. func GetConsensusState(store sdk.KVStore, cdc codec.BinaryCodec, height exported.Height) (*ConsensusState, error) { bz := store.Get(host.ConsensusStateKey(height)) - if bz == nil { + if len(bz) == 0 { return nil, sdkerrors.Wrapf( clienttypes.ErrConsensusStateNotFound, "consensus state does not exist for height %s", height, @@ -138,7 +138,7 @@ func SetProcessedTime(clientStore sdk.KVStore, height exported.Height, timeNs ui func GetProcessedTime(clientStore sdk.KVStore, height exported.Height) (uint64, bool) { key := ProcessedTimeKey(height) bz := clientStore.Get(key) - if bz == nil { + if len(bz) == 0 { return 0, false } return sdk.BigEndianToUint64(bz), true @@ -169,7 +169,7 @@ func SetProcessedHeight(clientStore sdk.KVStore, consHeight, processedHeight exp func GetProcessedHeight(clientStore sdk.KVStore, height exported.Height) (exported.Height, bool) { key := ProcessedHeightKey(height) bz := clientStore.Get(key) - if bz == nil { + if len(bz) == 0 { return nil, false } processedHeight, err := clienttypes.ParseHeight(string(bz)) @@ -320,7 +320,7 @@ func PruneAllExpiredConsensusStates( // Helper function for GetNextConsensusState and GetPreviousConsensusState func getTmConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, key []byte) (*ConsensusState, bool) { bz := clientStore.Get(key) - if bz == nil { + if len(bz) == 0 { return nil, false } diff --git a/modules/light-clients/09-localhost/types/client_state.go b/modules/light-clients/09-localhost/types/client_state.go index 961d84837b5..02ee525e473 100644 --- a/modules/light-clients/09-localhost/types/client_state.go +++ b/modules/light-clients/09-localhost/types/client_state.go @@ -121,7 +121,7 @@ func (cs ClientState) VerifyClientState( ) error { path := host.KeyClientState bz := store.Get([]byte(path)) - if bz == nil { + if len(bz) == 0 { return sdkerrors.Wrapf(clienttypes.ErrFailedClientStateVerification, "not found for path: %s", path) } @@ -160,7 +160,7 @@ func (cs ClientState) VerifyConnectionState( ) error { path := host.ConnectionKey(connectionID) bz := store.Get(path) - if bz == nil { + if len(bz) == 0 { return sdkerrors.Wrapf(clienttypes.ErrFailedConnectionStateVerification, "not found for path %s", path) } @@ -194,7 +194,7 @@ func (cs ClientState) VerifyChannelState( ) error { path := host.ChannelKey(portID, channelID) bz := store.Get(path) - if bz == nil { + if len(bz) == 0 { return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "not found for path %s", path) }