Skip to content

Commit

Permalink
chore: Add HasConnection and HasChannel methods. (#3082)
Browse files Browse the repository at this point in the history
(cherry picked from commit b2fb119)
  • Loading branch information
fedekunze authored and mergify[bot] committed Jan 31, 2023
1 parent 4eacfed commit a67735d
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (core) [\#3082](https://github.com/cosmos/ibc-go/pull/3082) Add `HasConnection` and `HasChannel` methods.
* (tests) [\#2926](https://github.com/cosmos/ibc-go/pull/2926) Lint tests
* (apps/transfer) [\#2643](https://github.com/cosmos/ibc-go/pull/2643) Add amount, denom, and memo to transfer event emission.
* (core) [\#2746](https://github.com/cosmos/ibc-go/pull/2746) Allow proof height to be zero for all core IBC `sdk.Msg` types that contain proofs.
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
2 changes: 1 addition & 1 deletion modules/core/02-client/migrations/v7/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func handleSolomachineMigration(ctx sdk.Context, store sdk.KVStore, cdc codec.Bi
clientStore := clientKeeper.ClientStore(ctx, clientID)

bz := clientStore.Get(host.ClientStateKey())
if bz == nil {
if len(bz) == 0 {
return sdkerrors.Wrapf(clienttypes.ErrClientNotFound, "clientID %s", clientID)
}

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 @@ -106,7 +113,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 @@ -127,7 +134,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/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func setConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensus
// If the ConsensusState does not exist in state for the provided height a nil value and false boolean flag is returned
func GetConsensusState(store sdk.KVStore, cdc codec.BinaryCodec, height exported.Height) (*ConsensusState, bool) {
bz := store.Get(host.ConsensusStateKey(height))
if bz == nil {
if len(bz) == 0 {
return nil, false
}

Expand Down Expand Up @@ -129,7 +129,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 @@ -160,7 +160,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 @@ -306,7 +306,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

0 comments on commit a67735d

Please sign in to comment.