|
| 1 | +package v5_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" |
| 7 | + "github.com/stretchr/testify/suite" |
| 8 | + |
| 9 | + v5 "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/migrations/v5" |
| 10 | + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" |
| 11 | + icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" |
| 12 | + channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" |
| 13 | + host "github.com/cosmos/ibc-go/v5/modules/core/24-host" |
| 14 | + ibctesting "github.com/cosmos/ibc-go/v5/testing" |
| 15 | + ibcmock "github.com/cosmos/ibc-go/v5/testing/mock" |
| 16 | +) |
| 17 | + |
| 18 | +type MigrationsTestSuite struct { |
| 19 | + suite.Suite |
| 20 | + |
| 21 | + chainA *ibctesting.TestChain |
| 22 | + chainB *ibctesting.TestChain |
| 23 | + |
| 24 | + coordinator *ibctesting.Coordinator |
| 25 | + path *ibctesting.Path |
| 26 | +} |
| 27 | + |
| 28 | +func (suite *MigrationsTestSuite) SetupTest() { |
| 29 | + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) |
| 30 | + |
| 31 | + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1)) |
| 32 | + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2)) |
| 33 | + |
| 34 | + suite.path = ibctesting.NewPath(suite.chainA, suite.chainB) |
| 35 | + suite.path.EndpointA.ChannelConfig.PortID = icatypes.PortID |
| 36 | + suite.path.EndpointB.ChannelConfig.PortID = icatypes.PortID |
| 37 | + suite.path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED |
| 38 | + suite.path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED |
| 39 | + suite.path.EndpointA.ChannelConfig.Version = icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) |
| 40 | + suite.path.EndpointB.ChannelConfig.Version = icatypes.NewDefaultMetadataString(ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) |
| 41 | +} |
| 42 | + |
| 43 | +func (suite *MigrationsTestSuite) SetupPath() error { |
| 44 | + if err := suite.RegisterInterchainAccount(suite.path.EndpointA, ibctesting.TestAccAddress); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + if err := suite.path.EndpointB.ChanOpenTry(); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + if err := suite.path.EndpointA.ChanOpenAck(); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + if err := suite.path.EndpointB.ChanOpenConfirm(); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (suite *MigrationsTestSuite) RegisterInterchainAccount(endpoint *ibctesting.Endpoint, owner string) error { |
| 64 | + portID, err := icatypes.NewControllerPortID(owner) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + channelSequence := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(endpoint.Chain.GetContext()) |
| 70 | + |
| 71 | + if err := endpoint.Chain.GetSimApp().ICAControllerKeeper.RegisterInterchainAccount(endpoint.Chain.GetContext(), endpoint.ConnectionID, owner, endpoint.ChannelConfig.Version); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + // commit state changes for proof verification |
| 76 | + endpoint.Chain.NextBlock() |
| 77 | + |
| 78 | + // update port/channel ids |
| 79 | + endpoint.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) |
| 80 | + endpoint.ChannelConfig.PortID = portID |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func TestKeeperTestSuite(t *testing.T) { |
| 86 | + suite.Run(t, new(MigrationsTestSuite)) |
| 87 | +} |
| 88 | + |
| 89 | +func (suite *MigrationsTestSuite) TestMigrateICS27ChannelCapability() { |
| 90 | + suite.SetupTest() |
| 91 | + suite.coordinator.SetupConnections(suite.path) |
| 92 | + |
| 93 | + err := suite.SetupPath() |
| 94 | + suite.Require().NoError(err) |
| 95 | + |
| 96 | + // create and claim a new capability with ibc/mock for "channel-1" |
| 97 | + // note: suite.SetupPath() now claims the chanel capability using icacontroller for "channel-0" |
| 98 | + capName := host.ChannelCapabilityPath(suite.path.EndpointA.ChannelConfig.PortID, channeltypes.FormatChannelIdentifier(1)) |
| 99 | + |
| 100 | + cap, err := suite.chainA.GetSimApp().ScopedIBCKeeper.NewCapability(suite.chainA.GetContext(), capName) |
| 101 | + suite.Require().NoError(err) |
| 102 | + |
| 103 | + err = suite.chainA.GetSimApp().ScopedICAMockKeeper.ClaimCapability(suite.chainA.GetContext(), cap, capName) |
| 104 | + suite.Require().NoError(err) |
| 105 | + |
| 106 | + // assert the capability is owned by the mock module |
| 107 | + cap, found := suite.chainA.GetSimApp().ScopedICAMockKeeper.GetCapability(suite.chainA.GetContext(), capName) |
| 108 | + suite.Require().NotNil(cap) |
| 109 | + suite.Require().True(found) |
| 110 | + |
| 111 | + isAuthenticated := suite.chainA.GetSimApp().ScopedICAMockKeeper.AuthenticateCapability(suite.chainA.GetContext(), cap, capName) |
| 112 | + suite.Require().True(isAuthenticated) |
| 113 | + |
| 114 | + cap, found = suite.chainA.GetSimApp().ScopedICAControllerKeeper.GetCapability(suite.chainA.GetContext(), capName) |
| 115 | + suite.Require().Nil(cap) |
| 116 | + suite.Require().False(found) |
| 117 | + |
| 118 | + suite.ResetMemStore() // empty the x/capability in-memory store |
| 119 | + |
| 120 | + err = v5.MigrateICS27ChannelCapability( |
| 121 | + suite.chainA.GetContext(), |
| 122 | + suite.chainA.Codec, |
| 123 | + suite.chainA.GetSimApp().GetKey(capabilitytypes.StoreKey), |
| 124 | + suite.chainA.GetSimApp().CapabilityKeeper, |
| 125 | + ibcmock.ModuleName+types.SubModuleName, |
| 126 | + ) |
| 127 | + |
| 128 | + suite.Require().NoError(err) |
| 129 | + |
| 130 | + // assert the capability is now owned by the ICS27 controller submodule |
| 131 | + cap, found = suite.chainA.GetSimApp().ScopedICAControllerKeeper.GetCapability(suite.chainA.GetContext(), capName) |
| 132 | + suite.Require().NotNil(cap) |
| 133 | + suite.Require().True(found) |
| 134 | + |
| 135 | + isAuthenticated = suite.chainA.GetSimApp().ScopedICAControllerKeeper.AuthenticateCapability(suite.chainA.GetContext(), cap, capName) |
| 136 | + suite.Require().True(isAuthenticated) |
| 137 | + |
| 138 | + cap, found = suite.chainA.GetSimApp().ScopedICAMockKeeper.GetCapability(suite.chainA.GetContext(), capName) |
| 139 | + suite.Require().Nil(cap) |
| 140 | + suite.Require().False(found) |
| 141 | + |
| 142 | + // ensure channel capability for "channel-0" is still owned by the controller |
| 143 | + capName = host.ChannelCapabilityPath(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID) |
| 144 | + cap, found = suite.chainA.GetSimApp().ScopedICAControllerKeeper.GetCapability(suite.chainA.GetContext(), capName) |
| 145 | + suite.Require().NotNil(cap) |
| 146 | + suite.Require().True(found) |
| 147 | + |
| 148 | + isAuthenticated = suite.chainA.GetSimApp().ScopedICAControllerKeeper.AuthenticateCapability(suite.chainA.GetContext(), cap, capName) |
| 149 | + suite.Require().True(isAuthenticated) |
| 150 | +} |
| 151 | + |
| 152 | +// ResetMemstore removes all existing fwd and rev capability kv pairs and deletes `KeyMemInitialised` from the x/capability memstore. |
| 153 | +// This effectively mocks a new chain binary being started. Migration code is run against persisted state only and allows the memstore to be reinitialised. |
| 154 | +func (suite *MigrationsTestSuite) ResetMemStore() { |
| 155 | + memStore := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetMemKey(capabilitytypes.MemStoreKey)) |
| 156 | + memStore.Delete(capabilitytypes.KeyMemInitialized) |
| 157 | + |
| 158 | + iterator := memStore.Iterator(nil, nil) |
| 159 | + defer iterator.Close() |
| 160 | + |
| 161 | + for ; iterator.Valid(); iterator.Next() { |
| 162 | + memStore.Delete(iterator.Key()) |
| 163 | + } |
| 164 | +} |
0 commit comments