-
Notifications
You must be signed in to change notification settings - Fork 637
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
chore: adding assertion of channel capabilities migration #2140
Merged
Merged
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a6575eb
wip initial commit
damiannolan 94091cf
draft migration completed
damiannolan 3c95389
removing unnecessary storekey arg
damiannolan 2bb7c3f
additional cleanup
damiannolan d546d20
adding updates to migrations and tests additional assertions
damiannolan a79d10e
updating and moving migrations code
damiannolan c2da8b2
adding additional checks to tests
damiannolan 6ea2014
cleaning up tests
damiannolan c981bcc
cleaning up tests
damiannolan 88124f4
updating inline doc comments, checking err return
damiannolan cf3bfaf
Merge branch 'main' into damian/ics27-chan-capability-migrations
damiannolan 443b548
using InitMemStore in favour of InitializeCapability, adjusting tests
damiannolan bdec1bc
Merge branch 'damian/ics27-chan-capability-migrations' of github.com:…
damiannolan 5229159
adding assertion of channel capabilities migration
damiannolan 478521e
Merge branch 'main' into damian/ics27-assert-chan-capabilities
damiannolan bf88e6d
adapting migration code to use get/authenticate capability
damiannolan 8949fa9
adding changelog
damiannolan 0bd27e9
updating tests
damiannolan dda0398
set middleware enabled for existing channels
damiannolan 49df7dd
assert middleware enabled in tests
damiannolan 4fc9aae
updating changelog with middleware enabled flag
damiannolan 8cd3d49
Merge branch 'main' into damian/ics27-assert-chan-capabilities
damiannolan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
modules/apps/27-interchain-accounts/controller/keeper/migrations.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
|
||
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" | ||
host "github.com/cosmos/ibc-go/v5/modules/core/24-host" | ||
) | ||
|
||
// Migrator is a struct for handling in-place store migrations. | ||
type Migrator struct { | ||
keeper *Keeper | ||
} | ||
|
||
// NewMigrator returns a new Migrator. | ||
func NewMigrator(keeper *Keeper) Migrator { | ||
return Migrator{keeper: keeper} | ||
} | ||
|
||
// 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 _, channel := range m.keeper.GetAllActiveChannels(ctx) { | ||
name := host.ChannelCapabilityPath(channel.PortId, channel.ChannelId) | ||
owners, found := m.keeper.scopedKeeper.GetOwners(ctx, name) | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !found { | ||
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityOwnersNotFound, "failed to find capability owners for: %s", name) | ||
} | ||
|
||
ibcOwner := capabilitytypes.NewOwner(host.ModuleName, name) | ||
if index, found := owners.Get(ibcOwner); !found && index != 0 { | ||
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityNotOwned, "expected capability owner: %s", host.ModuleName) | ||
} | ||
|
||
controllerOwner := capabilitytypes.NewOwner(types.SubModuleName, name) | ||
if index, found := owners.Get(controllerOwner); !found && index != 1 { | ||
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityNotOwned, "expected capability owner: %s", types.SubModuleName) | ||
} | ||
} | ||
|
||
return nil | ||
} |
38 changes: 38 additions & 0 deletions
38
modules/apps/27-interchain-accounts/controller/keeper/migrations_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper" | ||
ibctesting "github.com/cosmos/ibc-go/v5/testing" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestAssertChannelCapabilityMigrations() { | ||
testCases := []struct { | ||
name string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"success", | ||
func() {}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
path := NewICAPath(suite.chainA, suite.chainB) | ||
suite.coordinator.SetupConnections(path) | ||
|
||
err := SetupICAPath(path, ibctesting.TestAccAddress) | ||
suite.Require().NoError(err) | ||
|
||
tc.malleate() | ||
|
||
migrator := keeper.NewMigrator(&suite.chainA.GetSimApp().ICAControllerKeeper) | ||
err = migrator.MigrateCapabilitiesConfirm(suite.chainA.GetContext()) | ||
suite.Require().NoError(err) | ||
}) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
modules/apps/27-interchain-accounts/controller/migrations/v5/migrations.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package v5 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
|
||
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" | ||
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" | ||
host "github.com/cosmos/ibc-go/v5/modules/core/24-host" | ||
) | ||
|
||
// MigrateICS27ChannelCapability performs a search on a prefix store using the provided store key and module name. | ||
// It retrieves the associated channel capability index and reassigns ownership to the ICS27 controller submodule. | ||
func MigrateICS27ChannelCapability( | ||
ctx sdk.Context, | ||
memStoreKey storetypes.StoreKey, | ||
capabilityKeeper *capabilitykeeper.Keeper, | ||
module string, | ||
) error { | ||
// construct a prefix store using the x/capability reverse lookup key: {module}/rev/{name} -> index | ||
keyPrefix := capabilitytypes.RevCapabilityKey(module, fmt.Sprintf("%s/%s/%s", host.KeyChannelCapabilityPrefix, host.KeyPortPrefix, icatypes.PortPrefix)) | ||
prefixStore := prefix.NewStore(ctx.KVStore(memStoreKey), keyPrefix) | ||
|
||
iterator := sdk.KVStorePrefixIterator(prefixStore, nil) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
// unmarshal the capability index value and retrieve the set of owners | ||
index := sdk.BigEndianToUint64(iterator.Value()) | ||
owners, found := capabilityKeeper.GetOwners(ctx, index) | ||
if !found { | ||
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityOwnersNotFound, "no owners found for capability at index: %d", index) | ||
} | ||
|
||
// reconstruct the capability name using the prefixes and iterator key | ||
name := fmt.Sprintf("%s/%s/%s%s", host.KeyChannelCapabilityPrefix, host.KeyPortPrefix, icatypes.PortPrefix, string(iterator.Key())) | ||
prevOwner := capabilitytypes.NewOwner(module, name) | ||
newOwner := capabilitytypes.NewOwner(types.SubModuleName, name) | ||
|
||
// remove the existing module owner | ||
owners.Remove(prevOwner) | ||
prefixStore.Delete(iterator.Key()) | ||
|
||
// add the controller submodule to the set of owners | ||
if err := owners.Set(newOwner); err != nil { | ||
return err | ||
} | ||
|
||
// set the new owners for the appropriate capability index | ||
capabilityKeeper.SetOwners(ctx, index, owners) | ||
|
||
// initialise the x/capability memstore | ||
capabilityKeeper.InitMemStore(ctx) | ||
} | ||
|
||
return nil | ||
} |
135 changes: 135 additions & 0 deletions
135
modules/apps/27-interchain-accounts/controller/migrations/v5/migrations_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package v5_test | ||
|
||
import ( | ||
"testing" | ||
|
||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
"github.com/stretchr/testify/suite" | ||
|
||
v5 "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/migrations/v5" | ||
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" | ||
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" | ||
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" | ||
host "github.com/cosmos/ibc-go/v5/modules/core/24-host" | ||
ibctesting "github.com/cosmos/ibc-go/v5/testing" | ||
ibcmock "github.com/cosmos/ibc-go/v5/testing/mock" | ||
) | ||
|
||
type MigrationsTestSuite struct { | ||
suite.Suite | ||
|
||
chainA *ibctesting.TestChain | ||
chainB *ibctesting.TestChain | ||
|
||
coordinator *ibctesting.Coordinator | ||
path *ibctesting.Path | ||
} | ||
|
||
func (suite *MigrationsTestSuite) SetupTest() { | ||
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) | ||
|
||
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1)) | ||
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2)) | ||
|
||
suite.path = ibctesting.NewPath(suite.chainA, suite.chainB) | ||
suite.path.EndpointA.ChannelConfig.PortID = icatypes.PortID | ||
suite.path.EndpointB.ChannelConfig.PortID = icatypes.PortID | ||
suite.path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED | ||
suite.path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED | ||
suite.path.EndpointA.ChannelConfig.Version = icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) | ||
suite.path.EndpointB.ChannelConfig.Version = icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) | ||
} | ||
|
||
func (suite *MigrationsTestSuite) SetupPath() error { | ||
if err := suite.RegisterInterchainAccount(suite.path.EndpointA, ibctesting.TestAccAddress); err != nil { | ||
return err | ||
} | ||
|
||
if err := suite.path.EndpointB.ChanOpenTry(); err != nil { | ||
return err | ||
} | ||
|
||
if err := suite.path.EndpointA.ChanOpenAck(); err != nil { | ||
return err | ||
} | ||
|
||
if err := suite.path.EndpointB.ChanOpenConfirm(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (suite *MigrationsTestSuite) RegisterInterchainAccount(endpoint *ibctesting.Endpoint, owner string) error { | ||
portID, err := icatypes.NewControllerPortID(owner) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
channelSequence := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(endpoint.Chain.GetContext()) | ||
|
||
if err := endpoint.Chain.GetSimApp().ICAControllerKeeper.RegisterInterchainAccount(endpoint.Chain.GetContext(), endpoint.ConnectionID, owner, endpoint.ChannelConfig.Version); err != nil { | ||
return err | ||
} | ||
|
||
// commit state changes for proof verification | ||
endpoint.Chain.NextBlock() | ||
|
||
// update port/channel ids | ||
endpoint.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) | ||
endpoint.ChannelConfig.PortID = portID | ||
|
||
return nil | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(MigrationsTestSuite)) | ||
} | ||
|
||
func (suite *MigrationsTestSuite) TestMigrateICS27ChannelCapability() { | ||
suite.SetupTest() | ||
suite.coordinator.SetupConnections(suite.path) | ||
|
||
err := suite.SetupPath() | ||
suite.Require().NoError(err) | ||
|
||
capName := host.ChannelCapabilityPath(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID) | ||
|
||
// assert the capability is owned by the mock module | ||
cap, found := suite.chainA.GetSimApp().ScopedICAMockKeeper.GetCapability(suite.chainA.GetContext(), capName) | ||
suite.Require().NotNil(cap) | ||
suite.Require().True(found) | ||
|
||
isAuthenticated := suite.chainA.GetSimApp().ScopedICAMockKeeper.AuthenticateCapability(suite.chainA.GetContext(), cap, capName) | ||
suite.Require().True(isAuthenticated) | ||
|
||
cap, found = suite.chainA.GetSimApp().ScopedICAControllerKeeper.GetCapability(suite.chainA.GetContext(), capName) | ||
suite.Require().Nil(cap) | ||
suite.Require().False(found) | ||
|
||
// manually delete the `KeyMemInitialised` from the x/capability memstore | ||
// this allows capabilityKeeper.InitMemStore(ctx) to re-initialise capabilities | ||
memStore := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetMemKey(capabilitytypes.MemStoreKey)) | ||
memStore.Delete(capabilitytypes.KeyMemInitialized) | ||
|
||
err = v5.MigrateICS27ChannelCapability( | ||
suite.chainA.GetContext(), | ||
suite.chainA.GetSimApp().GetMemKey(capabilitytypes.MemStoreKey), | ||
suite.chainA.GetSimApp().CapabilityKeeper, | ||
ibcmock.ModuleName+types.SubModuleName, | ||
) | ||
|
||
suite.Require().NoError(err) | ||
|
||
// assert the capability is now owned by the ICS27 controller submodule | ||
cap, found = suite.chainA.GetSimApp().ScopedICAControllerKeeper.GetCapability(suite.chainA.GetContext(), capName) | ||
suite.Require().NotNil(cap) | ||
suite.Require().True(found) | ||
|
||
isAuthenticated = suite.chainA.GetSimApp().ScopedICAControllerKeeper.AuthenticateCapability(suite.chainA.GetContext(), cap, capName) | ||
suite.Require().True(isAuthenticated) | ||
|
||
cap, found = suite.chainA.GetSimApp().ScopedICAMockKeeper.GetCapability(suite.chainA.GetContext(), capName) | ||
suite.Require().Nil(cap) | ||
suite.Require().False(found) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to suggestions on improving the name of this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the name