Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding GetChannelsWithPortPrefix function to 04-channel #2304

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
74dbd34
feat: adding GetChannelsWithPortPrefix functionto 04-channel
chatton Sep 19, 2022
ebbcd36
chore: adding changelog entry
chatton Sep 19, 2022
b5e1e9f
chore: addressing PR feedback
chatton Sep 20, 2022
b8bbe7d
chore: merge main
chatton Sep 20, 2022
9001e64
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 21, 2022
691748b
chore: merge
chatton Sep 22, 2022
137439f
chore: fixing unit test
chatton Sep 22, 2022
2ffd5e4
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 22, 2022
fc0c1a4
chore: addressing PR feedback
chatton Sep 26, 2022
b44777e
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 26, 2022
5bd25fd
chore: adding test [wip]
chatton Sep 27, 2022
019afc6
chore: adding tests for GetChannelsWithPortPrefix
chatton Sep 29, 2022
1b663db
chore: reame GetChannelsWithPortPrefix to GetAllChannelsWithPortPrefix
chatton Sep 29, 2022
2707071
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 29, 2022
78697b5
Update modules/apps/27-interchain-accounts/controller/keeper/migratio…
chatton Sep 29, 2022
e3e4fc5
chore: updated variable references
chatton Sep 29, 2022
afe7941
chore: renaming Id to ID
chatton Sep 29, 2022
f404c27
chore: addressing pr feedback
chatton Sep 29, 2022
ba22f69
chore: addressing PR feedback and adding contains function to verify …
chatton Sep 29, 2022
d287003
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
colin-axner Sep 29, 2022
0e61f33
chore: adding allChannels as variable in tests
chatton Sep 29, 2022
d7ee861
Merge branch 'cian/issue#2245-add-port-prefix-iterator-function-to-04…
chatton Sep 29, 2022
b085b89
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 29, 2022
bd9ded4
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 29, 2022
4f695dc
chore: fixing migrations test
chatton Sep 29, 2022
c8364f2
Merge branch 'main' into cian/issue#2245-add-port-prefix-iterator-fun…
chatton Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (apps/27-interchain-accounts) [\#2157](https://github.com/cosmos/ibc-go/pull/2157) Adding `IsMiddlewareEnabled` functionality to enforce calls to ICS27 msg server to *not* route to the underlying application.
* (apps/27-interchain-accounts) [\#2177](https://github.com/cosmos/ibc-go/pull/2177) Adding `IsMiddlewareEnabled` flag to interchain accounts `ActiveChannel` genesis type.
* (apps/27-interchain-accounts) [\#2140](https://github.com/cosmos/ibc-go/pull/2140) Adding migration handler to ICS27 `controller` submodule to assert ownership of channel capabilities and set middleware enabled flag for existing channels. The ICS27 module consensus version has been bumped from 1 to 2.
* (core/04-channel) [\#2304](https://github.com/cosmos/ibc-go/pull/2304) Adding `GetChannelsWithPortPrefix` function which filters channels based on a provided port prefix.

### Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

"github.com/cosmos/ibc-go/v5/modules/core/23-commitment/types"
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types"
commitmenttypes "github.com/cosmos/ibc-go/v5/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v5/modules/core/24-host"
)

Expand All @@ -22,7 +23,8 @@ func NewMigrator(keeper *Keeper) Migrator {
// AssertChannelCapabilityMigrations checks that all channel capabilities generated using the interchain accounts controller port prefix
// are owned by the controller submodule and ibc.
func (m Migrator) AssertChannelCapabilityMigrations(ctx sdk.Context) error {
for _, ch := range m.keeper.GetAllActiveChannels(ctx) {
filteredChannels := m.keeper.channelKeeper.GetChannelsWithPortPrefix(ctx, icatypes.PortPrefix)
for _, ch := range filteredChannels {
name := host.ChannelCapabilityPath(ch.PortId, ch.ChannelId)
cap, found := m.keeper.scopedKeeper.GetCapability(ctx, name)
if !found {
Expand All @@ -31,7 +33,7 @@ func (m Migrator) AssertChannelCapabilityMigrations(ctx sdk.Context) error {

isAuthenticated := m.keeper.scopedKeeper.AuthenticateCapability(ctx, cap, name)
if !isAuthenticated {
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityNotOwned, "expected capability owner: %s", types.SubModuleName)
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityNotOwned, "expected capability owner: %s", commitmenttypes.SubModuleName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how this ended up as commitmenttypes I see the import previously would've been incorrect also.
It should be controllertypes.SubModuleName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I was wondering about that actually, I changed the import to be inline with other imports in the file. I can update this now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the dangers of types as the import alias.

}

m.keeper.SetMiddlewareEnabled(ctx, ch.PortId, ch.ChannelId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package keeper_test

import (
"fmt"

"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper"
controllertypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v5/testing"
)

Expand All @@ -16,15 +20,19 @@ func (suite *KeeperTestSuite) TestAssertChannelCapabilityMigrations() {
func() {},
true,
},
{
"channel with different port is filtered out",
func() {
portIdWithOutPrefix := ibctesting.MockPort
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), portIdWithOutPrefix, ibctesting.FirstChannelID, channeltypes.Channel{})
},
true,
},
{
"capability not found",
func() {
suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(
suite.chainA.GetContext(),
ibctesting.FirstConnectionID,
ibctesting.MockPort,
ibctesting.FirstChannelID,
)
portIdWithPrefix := fmt.Sprintf("%s-%s", controllertypes.SubModuleName, TestAccAddress)
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), portIdWithPrefix, ibctesting.FirstChannelID, channeltypes.Channel{})
},
false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ChannelKeeper interface {
GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool)
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
GetConnection(ctx sdk.Context, connectionID string) (ibcexported.ConnectionI, error)
GetChannelsWithPortPrefix(ctx sdk.Context, portPrefix string) []channeltypes.IdentifiedChannel
}

// PortKeeper defines the expected IBC port keeper
Expand Down
24 changes: 24 additions & 0 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -384,6 +385,24 @@ func (k Keeper) IterateChannels(ctx sdk.Context, cb func(types.IdentifiedChannel
}
}

// GetChannelsWithPortPrefix returns all channels with the specified port prefix.
func (k Keeper) GetChannelsWithPortPrefix(ctx sdk.Context, portPrefix string) []types.IdentifiedChannel {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider naming this function to align with GetAllChannels below which returns []types.IdentifiedChannel also?

Maybe GetAllChannelsWithPortPrefix or GetAllPortPrefixedChannels?

I don't feel particularly strongly but just a thought. cc. @crodriguezvega @colin-axner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like GetAllChannelsWithPortPrefix to stay consistent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. I have no preference

store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, filteredPortPrefix(portPrefix))
defer iterator.Close()

var filteredChannels []types.IdentifiedChannel
for ; iterator.Valid(); iterator.Next() {
var channel types.Channel
k.cdc.MustUnmarshal(iterator.Value(), &channel)

portID, channelID := host.MustParseChannelPath(string(iterator.Key()))
identifiedChannel := types.NewIdentifiedChannel(portID, channelID, channel)
filteredChannels = append(filteredChannels, identifiedChannel)
}
return filteredChannels
}

// GetAllChannels returns all stored Channel objects.
func (k Keeper) GetAllChannels(ctx sdk.Context) (channels []types.IdentifiedChannel) {
k.IterateChannels(ctx, func(channel types.IdentifiedChannel) bool {
Expand Down Expand Up @@ -469,3 +488,8 @@ func (k Keeper) iterateHashes(_ sdk.Context, iterator db.Iterator, cb func(portI
}
}
}

// filteredPortPrefix returns the prefix key for the given port prefix.
func filteredPortPrefix(portPrefix string) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to place this function in types/keys.go instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion, done!

return []byte(fmt.Sprintf("%s/ports/%s", host.KeyChannelEndPrefix, portPrefix))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit:

Suggested change
return []byte(fmt.Sprintf("%s/ports/%s", host.KeyChannelEndPrefix, portPrefix))
return []byte(fmt.Sprintf("%s/%s/%s", host.KeyChannelEndPrefix, host.KeyPortPrefix, portPrefix))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

}