Skip to content

Commit

Permalink
imp(channel, connection): add Has function for ConnectionEnd and Chan…
Browse files Browse the repository at this point in the history
…nel (backport #3082) (#3561)

* chore: Add `HasConnection` and `HasChannel` methods. (#3082)

(cherry picked from commit b2fb119)

# Conflicts:
#	CHANGELOG.md
#	modules/core/02-client/migrations/v7/store.go
#	modules/light-clients/07-tendermint/types/store.go

* fix conflicts

---------

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
  • Loading branch information
3 people authored May 15, 2023
1 parent 94221f7 commit 03ef25e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion modules/apps/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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")
}

Expand Down
13 changes: 10 additions & 3 deletions modules/core/03-connection/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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")
}

Expand Down
20 changes: 13 additions & 7 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/core/04-channel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions modules/light-clients/07-tendermint/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions modules/light-clients/09-localhost/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 03ef25e

Please sign in to comment.