From b39d855b1e141327890824f125e2c095e3580004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 24 Nov 2022 12:17:50 +0100 Subject: [PATCH 01/12] feat: add genesis migrations for v7 --- .../core/02-client/migrations/v7/genesis.go | 82 +++++++++++ .../02-client/migrations/v7/genesis_test.go | 128 ++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 modules/core/02-client/migrations/v7/genesis.go create mode 100644 modules/core/02-client/migrations/v7/genesis_test.go diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go new file mode 100644 index 00000000000..314e0b8e233 --- /dev/null +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -0,0 +1,82 @@ +package v7 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + + "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" + "github.com/cosmos/ibc-go/v6/modules/core/exported" +) + +// MigrateGenesis accepts an exported IBC client genesis file and migrates it to: +// +// - Update solo machine client state protobuf definition (v2 to v3) +// - Remove all solo machine consensus states +// - Remove localhost client +func MigrateGenesis(clientGenState *types.GenesisState, cdc codec.BinaryCodec) (*types.GenesisState, error) { + // To prune the client and consensus states, we will create new slices to fill up + // with information we want to keep. + var ( + clientsConsensus []types.ClientConsensusStates + clients []types.IdentifiedClientState + ) + + for _, client := range clientGenState.Clients { + clientType, _, err := types.ParseClientIdentifier(client.ClientId) + if err != nil { + return nil, err + } + + // update solo machine client state defintions + switch clientType { + case exported.Solomachine: + clientState := &ClientState{} + if err := cdc.Unmarshal(client.ClientState.Value, clientState); err != nil { + return nil, sdkerrors.Wrap(err, "failed to unmarshal client state bytes into solo machine client state") + } + + updatedClientState := migrateSolomachine(clientState) + + any, err := types.PackClientState(updatedClientState) + if err != nil { + return nil, err + } + + clients = append(clients, types.IdentifiedClientState{ + ClientId: client.ClientId, + ClientState: any, + }) + + case Localhost: + // remove localhost client state by not adding client state + + default: + // add all other client states + clients = append(clients, client) + } + + // iterate consensus states by client + for _, clientConsensusStates := range clientGenState.ClientsConsensus { + // look for consensus states for the current client + if clientConsensusStates.ClientId == client.ClientId { + switch clientType { + case exported.Solomachine: + // remove all consensus states for the solo machine + // do not add to new clientsConsensus + + case Localhost: + // remove all consensus states for the solo machine + // do not add to new clientsConsensus + + default: + // ensure all consensus states added for other client types + clientsConsensus = append(clientsConsensus, clientConsensusStates) + } + } + } + } + + clientGenState.Clients = clients + clientGenState.ClientsConsensus = clientsConsensus + return clientGenState, nil +} diff --git a/modules/core/02-client/migrations/v7/genesis_test.go b/modules/core/02-client/migrations/v7/genesis_test.go new file mode 100644 index 00000000000..d20d9feda81 --- /dev/null +++ b/modules/core/02-client/migrations/v7/genesis_test.go @@ -0,0 +1,128 @@ +package v7_test + +import ( + "encoding/json" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + + ibcclient "github.com/cosmos/ibc-go/v6/modules/core/02-client" + "github.com/cosmos/ibc-go/v6/modules/core/02-client/migrations/v7" + "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" + host "github.com/cosmos/ibc-go/v6/modules/core/24-host" + ibctesting "github.com/cosmos/ibc-go/v6/testing" +) + +func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { + // create tendermint clients + for i := 0; i < 3; i++ { + path := ibctesting.NewPath(suite.chainA, suite.chainB) + + suite.coordinator.SetupClients(path) + + err := path.EndpointA.UpdateClient() + suite.Require().NoError(err) + err = path.EndpointA.UpdateClient() + suite.Require().NoError(err) + } + + // create multiple legacy solo machine clients + solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-0", "testing", 1) + solomachineMulti := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-1", "testing", 4) + + clientGenState := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) + + // manually generate old proto buf definitions and set in genesis + // NOTE: we cannot use 'ExportGenesis' for the solo machines since we are + // using client states and consensus states which do not implement the exported.ClientState + // and exported.ConsensusState interface + var clients []types.IdentifiedClientState + for _, sm := range []*ibctesting.Solomachine{solomachine, solomachineMulti} { + clientState := sm.ClientState() + + // generate old client state proto definition + legacyClientState := &v7.ClientState{ + Sequence: clientState.Sequence, + ConsensusState: &v7.ConsensusState{ + PublicKey: clientState.ConsensusState.PublicKey, + Diversifier: clientState.ConsensusState.Diversifier, + Timestamp: clientState.ConsensusState.Timestamp, + }, + AllowUpdateAfterProposal: true, + } + + // set client state + any, err := codectypes.NewAnyWithValue(legacyClientState) + suite.Require().NoError(err) + suite.Require().NotNil(any) + + clients = append(clients, types.IdentifiedClientState{ + ClientId: sm.ClientID, + ClientState: any, + }) + + // set in store for ease of determining expected genesis + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), sm.ClientID) + bz, err := suite.chainA.App.AppCodec().MarshalInterface(legacyClientState) + suite.Require().NoError(err) + clientStore.Set(host.ClientStateKey(), bz) + + any, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) + suite.Require().NoError(err) + suite.Require().NotNil(any) + + // obtain marshalled bytes to set in client store + bz, err = suite.chainA.App.AppCodec().MarshalInterface(legacyClientState.ConsensusState) + suite.Require().NoError(err) + + var consensusStates []types.ConsensusStateWithHeight + + // set consensus states in store and genesis + for i := uint64(0); i < numCreations; i++ { + height := types.NewHeight(1, i) + clientStore.Set(host.ConsensusStateKey(height), bz) + consensusStates = append(consensusStates, types.ConsensusStateWithHeight{ + Height: height, + ConsensusState: any, + }) + } + + clientGenState.ClientsConsensus = append(clientGenState.ClientsConsensus, types.ClientConsensusStates{ + ClientId: sm.ClientID, + ConsensusStates: consensusStates, + }) + } + + // solo machine clients must come before tendermint in expected + clientGenState.Clients = append(clients, clientGenState.Clients...) + + // migrate store get expected genesis + // store migration and genesis migration should produce identical results + // NOTE: tendermint clients are not pruned in genesis so the test should not have expired tendermint clients + err := v7.MigrateStore(suite.chainA.GetContext(), suite.chainA.GetSimApp().GetKey(host.StoreKey), suite.chainA.App.AppCodec()) + suite.Require().NoError(err) + expectedClientGenState := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) + + migrated, err := v7.MigrateGenesis(&clientGenState, suite.chainA.App.Codec()) + suite.Require().NoError(err) + + bz, err := clientCtx.Codec.MarshalJSON(&expectedClientGenState) + suite.Require().NoError(err) + + // Indent the JSON bz correctly. + var jsonObj map[string]interface{} + err = json.Unmarshal(bz, &jsonObj) + suite.Require().NoError(err) + expectedIndentedBz, err := json.MarshalIndent(jsonObj, "", "\t") + suite.Require().NoError(err) + + bz, err = clientCtx.Codec.MarshalJSON(migrated) + suite.Require().NoError(err) + + // Indent the JSON bz correctly. + err = json.Unmarshal(bz, &jsonObj) + suite.Require().NoError(err) + indentedBz, err := json.MarshalIndent(jsonObj, "", "\t") + suite.Require().NoError(err) + + suite.Require().Equal(string(expectedIndentedBz), string(indentedBz)) +} From 03290c779a22381be741d768e18328206cc4dd37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 24 Nov 2022 12:36:29 +0100 Subject: [PATCH 02/12] fix: update genesis migration function at core IBC level --- .../core/02-client/migrations/v7/genesis.go | 2 +- modules/core/legacy/v100/genesis.go | 54 ------ modules/core/legacy/v100/genesis_test.go | 177 ------------------ modules/core/migrations/v7/genesis.go | 40 ++++ modules/core/migrations/v7/genesis_test.go | 156 +++++++++++++++ modules/core/migrations/v7/migrations.go | 44 ----- modules/core/migrations/v7/migrations_test.go | 116 ------------ 7 files changed, 197 insertions(+), 392 deletions(-) delete mode 100644 modules/core/legacy/v100/genesis.go delete mode 100644 modules/core/legacy/v100/genesis_test.go create mode 100644 modules/core/migrations/v7/genesis.go create mode 100644 modules/core/migrations/v7/genesis_test.go delete mode 100644 modules/core/migrations/v7/migrations.go delete mode 100644 modules/core/migrations/v7/migrations_test.go diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go index 314e0b8e233..86d24960beb 100644 --- a/modules/core/02-client/migrations/v7/genesis.go +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -13,7 +13,7 @@ import ( // - Update solo machine client state protobuf definition (v2 to v3) // - Remove all solo machine consensus states // - Remove localhost client -func MigrateGenesis(clientGenState *types.GenesisState, cdc codec.BinaryCodec) (*types.GenesisState, error) { +func MigrateGenesis(clientGenState *types.GenesisState, cdc codec.ProtoCodecMarshaler) (*types.GenesisState, error) { // To prune the client and consensus states, we will create new slices to fill up // with information we want to keep. var ( diff --git a/modules/core/legacy/v100/genesis.go b/modules/core/legacy/v100/genesis.go deleted file mode 100644 index 668d8a1f78a..00000000000 --- a/modules/core/legacy/v100/genesis.go +++ /dev/null @@ -1,54 +0,0 @@ -package v100 - -import ( - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" - tmtypes "github.com/tendermint/tendermint/types" - - clientv100 "github.com/cosmos/ibc-go/v6/modules/core/02-client/legacy/v100" - clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" - connectiontypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types" - host "github.com/cosmos/ibc-go/v6/modules/core/24-host" - "github.com/cosmos/ibc-go/v6/modules/core/types" -) - -// MigrateGenesis accepts exported v1.0.0 IBC client genesis file and migrates it to: -// -// - Update solo machine client state protobuf definition (v1 to v2) -// - Remove all solo machine consensus states -// - Remove all expired tendermint consensus states -func MigrateGenesis(appState genutiltypes.AppMap, clientCtx client.Context, genDoc tmtypes.GenesisDoc, maxExpectedTimePerBlock uint64) (genutiltypes.AppMap, error) { - if appState[host.ModuleName] != nil { - // ensure legacy solo machines are registered - clientv100.RegisterInterfaces(clientCtx.InterfaceRegistry) - - // unmarshal relative source genesis application state - ibcGenState := &types.GenesisState{} - clientCtx.Codec.MustUnmarshalJSON(appState[host.ModuleName], ibcGenState) - - clientGenState, err := clientv100.MigrateGenesis(codec.NewProtoCodec(clientCtx.InterfaceRegistry), &ibcGenState.ClientGenesis, genDoc.GenesisTime, clienttypes.NewHeight(clienttypes.ParseChainID(genDoc.ChainID), uint64(genDoc.InitialHeight))) - if err != nil { - return nil, err - } - - ibcGenState.ClientGenesis = *clientGenState - - // set max expected time per block - connectionGenesis := connectiontypes.GenesisState{ - Connections: ibcGenState.ConnectionGenesis.Connections, - ClientConnectionPaths: ibcGenState.ConnectionGenesis.ClientConnectionPaths, - NextConnectionSequence: ibcGenState.ConnectionGenesis.NextConnectionSequence, - Params: connectiontypes.NewParams(maxExpectedTimePerBlock), - } - - ibcGenState.ConnectionGenesis = connectionGenesis - - // delete old genesis state - delete(appState, host.ModuleName) - - // set new ibc genesis state - appState[host.ModuleName] = clientCtx.Codec.MustMarshalJSON(ibcGenState) - } - return appState, nil -} diff --git a/modules/core/legacy/v100/genesis_test.go b/modules/core/legacy/v100/genesis_test.go deleted file mode 100644 index c9f1e800ce0..00000000000 --- a/modules/core/legacy/v100/genesis_test.go +++ /dev/null @@ -1,177 +0,0 @@ -package v100_test - -import ( - "testing" - - "github.com/cosmos/cosmos-sdk/client" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" - "github.com/stretchr/testify/suite" - tmtypes "github.com/tendermint/tendermint/types" - - ibcclient "github.com/cosmos/ibc-go/v6/modules/core/02-client" - clientv100 "github.com/cosmos/ibc-go/v6/modules/core/02-client/legacy/v100" - clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" - connectiontypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types" - host "github.com/cosmos/ibc-go/v6/modules/core/24-host" - v100 "github.com/cosmos/ibc-go/v6/modules/core/legacy/v100" - "github.com/cosmos/ibc-go/v6/modules/core/types" - ibctesting "github.com/cosmos/ibc-go/v6/testing" - "github.com/cosmos/ibc-go/v6/testing/simapp" -) - -type LegacyTestSuite struct { - suite.Suite - - coordinator *ibctesting.Coordinator - - // testing chains used for convenience and readability - chainA *ibctesting.TestChain - chainB *ibctesting.TestChain -} - -// TestLegacyTestSuite runs all the tests within this package. -func TestLegacyTestSuite(t *testing.T) { - suite.Run(t, new(LegacyTestSuite)) -} - -// SetupTest creates a coordinator with 2 test chains. -func (suite *LegacyTestSuite) SetupTest() { - suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) - suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1)) - suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2)) - // commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1) - suite.coordinator.CommitNBlocks(suite.chainA, 2) - suite.coordinator.CommitNBlocks(suite.chainB, 2) -} - -// NOTE: this test is mainly copied from 02-client/legacy/v100 -func (suite *LegacyTestSuite) TestMigrateGenesisSolomachine() { - path := ibctesting.NewPath(suite.chainA, suite.chainB) - encodingConfig := simapp.MakeTestEncodingConfig() - clientCtx := client.Context{}. - WithInterfaceRegistry(encodingConfig.InterfaceRegistry). - WithTxConfig(encodingConfig.TxConfig). - WithCodec(encodingConfig.Marshaler) - - // create multiple legacy solo machine clients - solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-0", "testing", 1) - solomachineMulti := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-1", "testing", 4) - - // create tendermint clients - // NOTE: only 1 set of metadata is created, we aren't testing ordering - // The purpose of this test is to ensure the genesis states can be marshalled/unmarshalled - suite.coordinator.SetupClients(path) - clientGenState := ibcclient.ExportGenesis(path.EndpointA.Chain.GetContext(), path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper) - - // manually generate old proto buf definitions and set in genesis - // NOTE: we cannot use 'ExportGenesis' for the solo machines since we are - // using client states and consensus states which do not implement the exported.ClientState - // and exported.ConsensusState interface - var clients []clienttypes.IdentifiedClientState - for _, sm := range []*ibctesting.Solomachine{solomachine, solomachineMulti} { - clientState := sm.ClientState() - - var seq uint64 - if clientState.IsFrozen { - seq = 1 - } - - // generate old client state proto definition - legacyClientState := &clientv100.ClientState{ - Sequence: clientState.Sequence, - FrozenSequence: seq, - ConsensusState: &clientv100.ConsensusState{ - PublicKey: clientState.ConsensusState.PublicKey, - Diversifier: clientState.ConsensusState.Diversifier, - Timestamp: clientState.ConsensusState.Timestamp, - }, - } - - // set client state - any, err := codectypes.NewAnyWithValue(legacyClientState) - suite.Require().NoError(err) - suite.Require().NotNil(any) - client := clienttypes.IdentifiedClientState{ - ClientId: sm.ClientID, - ClientState: any, - } - clients = append(clients, client) - - // set in store for ease of determining expected genesis - clientStore := path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper.ClientStore(path.EndpointA.Chain.GetContext(), sm.ClientID) - bz, err := path.EndpointA.Chain.App.AppCodec().MarshalInterface(legacyClientState) - suite.Require().NoError(err) - clientStore.Set(host.ClientStateKey(), bz) - - // set some consensus states - height1 := clienttypes.NewHeight(0, 1) - height2 := clienttypes.NewHeight(1, 2) - height3 := clienttypes.NewHeight(0, 123) - - any, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) - suite.Require().NoError(err) - suite.Require().NotNil(any) - consensusState1 := clienttypes.ConsensusStateWithHeight{ - Height: height1, - ConsensusState: any, - } - consensusState2 := clienttypes.ConsensusStateWithHeight{ - Height: height2, - ConsensusState: any, - } - consensusState3 := clienttypes.ConsensusStateWithHeight{ - Height: height3, - ConsensusState: any, - } - - clientConsensusState := clienttypes.ClientConsensusStates{ - ClientId: sm.ClientID, - ConsensusStates: []clienttypes.ConsensusStateWithHeight{consensusState1, consensusState2, consensusState3}, - } - - clientGenState.ClientsConsensus = append(clientGenState.ClientsConsensus, clientConsensusState) - - // set in store for ease of determining expected genesis - bz, err = path.EndpointA.Chain.App.AppCodec().MarshalInterface(legacyClientState.ConsensusState) - suite.Require().NoError(err) - clientStore.Set(host.ConsensusStateKey(height1), bz) - clientStore.Set(host.ConsensusStateKey(height2), bz) - clientStore.Set(host.ConsensusStateKey(height3), bz) - } - // solo machine clients must come before tendermint in expected - clientGenState.Clients = append(clients, clientGenState.Clients...) - - // migrate store get expected genesis - // store migration and genesis migration should produce identical results - err := clientv100.MigrateStore(path.EndpointA.Chain.GetContext(), path.EndpointA.Chain.GetSimApp().GetKey(host.StoreKey), path.EndpointA.Chain.App.AppCodec()) - suite.Require().NoError(err) - expectedClientGenState := ibcclient.ExportGenesis(path.EndpointA.Chain.GetContext(), path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper) - - // NOTE: these lines are added in comparison to 02-client/legacy/v100 - // generate appState with old ibc genesis state - appState := genutiltypes.AppMap{} - ibcGenState := types.DefaultGenesisState() - ibcGenState.ClientGenesis = clientGenState - clientv100.RegisterInterfaces(clientCtx.InterfaceRegistry) - appState[host.ModuleName] = clientCtx.Codec.MustMarshalJSON(ibcGenState) - genDoc := tmtypes.GenesisDoc{ - ChainID: suite.chainA.ChainID, - GenesisTime: suite.coordinator.CurrentTime, - InitialHeight: suite.chainA.GetContext().BlockHeight(), - } - - // NOTE: genesis time isn't updated since we aren't testing for tendermint consensus state pruning - migrated, err := v100.MigrateGenesis(appState, clientCtx, genDoc, uint64(connectiontypes.DefaultTimePerBlock)) - suite.Require().NoError(err) - - expectedAppState := genutiltypes.AppMap{} - expectedIBCGenState := types.DefaultGenesisState() - expectedIBCGenState.ClientGenesis = expectedClientGenState - - bz, err := clientCtx.Codec.MarshalJSON(expectedIBCGenState) - suite.Require().NoError(err) - expectedAppState[host.ModuleName] = bz - - suite.Require().Equal(expectedAppState, migrated) -} diff --git a/modules/core/migrations/v7/genesis.go b/modules/core/migrations/v7/genesis.go new file mode 100644 index 00000000000..0be575aa234 --- /dev/null +++ b/modules/core/migrations/v7/genesis.go @@ -0,0 +1,40 @@ +package v7 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" + + clientv7 "github.com/cosmos/ibc-go/v6/modules/core/02-client/migrations/v7" + host "github.com/cosmos/ibc-go/v6/modules/core/24-host" + "github.com/cosmos/ibc-go/v6/modules/core/types" +) + +// MigrateGenesis accepts an exported IBC client genesis file and migrates it to: +// +// - Update solo machine client state protobuf definition (v2 to v3) +// - Remove all solo machine consensus states +// - Remove any localhost clients +func MigrateGenesis(appState genutiltypes.AppMap, cdc codec.ProtoCodecMarshaler) (genutiltypes.AppMap, error) { + if appState[host.ModuleName] != nil { + // ensure legacy solo machines are registered + clientv7.RegisterInterfaces(cdc.InterfaceRegistry()) + + // unmarshal relative source genesis application state + ibcGenState := &types.GenesisState{} + cdc.MustUnmarshalJSON(appState[host.ModuleName], ibcGenState) + + clientGenState, err := clientv7.MigrateGenesis(&ibcGenState.ClientGenesis, cdc) + if err != nil { + return nil, err + } + + ibcGenState.ClientGenesis = *clientGenState + + // delete old genesis state + delete(appState, host.ModuleName) + + // set new ibc genesis state + appState[host.ModuleName] = cdc.MustMarshalJSON(ibcGenState) + } + return appState, nil +} diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go new file mode 100644 index 00000000000..428754fe99b --- /dev/null +++ b/modules/core/migrations/v7/genesis_test.go @@ -0,0 +1,156 @@ +package v7_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" + "github.com/stretchr/testify/suite" + + ibcclient "github.com/cosmos/ibc-go/v6/modules/core/02-client" + clientv7 "github.com/cosmos/ibc-go/v6/modules/core/02-client/migrations/v7" + clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" + host "github.com/cosmos/ibc-go/v6/modules/core/24-host" + v7 "github.com/cosmos/ibc-go/v6/modules/core/migrations/v7" + "github.com/cosmos/ibc-go/v6/modules/core/types" + ibctesting "github.com/cosmos/ibc-go/v6/testing" +) + +type MigrationsV7TestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain +} + +// TestMigrationsV7TestSuite runs all the tests within this package. +func TestMigrationsV7TestSuite(t *testing.T) { + suite.Run(t, new(MigrationsV7TestSuite)) +} + +// SetupTest creates a coordinator with 2 test chains. +func (suite *MigrationsV7TestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2)) +} + +// NOTE: this test is mainly copied from 02-client/migrations/v7/genesis_test.go +func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { + // create tendermint clients + for i := 0; i < 3; i++ { + path := ibctesting.NewPath(suite.chainA, suite.chainB) + + suite.coordinator.SetupClients(path) + + err := path.EndpointA.UpdateClient() + suite.Require().NoError(err) + err = path.EndpointA.UpdateClient() + suite.Require().NoError(err) + } + + // create multiple legacy solo machine clients + solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-0", "testing", 1) + solomachineMulti := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-1", "testing", 4) + + clientGenState := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) + + // manually generate old proto buf definitions and set in genesis + // NOTE: we cannot use 'ExportGenesis' for the solo machines since we are + // using client states and consensus states which do not implement the exported.ClientState + // and exported.ConsensusState interface + var clients []clienttypes.IdentifiedClientState + for _, sm := range []*ibctesting.Solomachine{solomachine, solomachineMulti} { + clientState := sm.ClientState() + + // generate old client state proto definition + legacyClientState := &v7.ClientState{ + Sequence: clientState.Sequence, + ConsensusState: &v7.ConsensusState{ + PublicKey: clientState.ConsensusState.PublicKey, + Diversifier: clientState.ConsensusState.Diversifier, + Timestamp: clientState.ConsensusState.Timestamp, + }, + AllowUpdateAfterProposal: true, + } + + // set client state + any, err := codectypes.NewAnyWithValue(legacyClientState) + suite.Require().NoError(err) + suite.Require().NotNil(any) + + clients = append(clients, clienttypes.IdentifiedClientState{ + ClientId: sm.ClientID, + ClientState: any, + }) + + // set in store for ease of determining expected genesis + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), sm.ClientID) + bz, err := suite.chainA.App.AppCodec().MarshalInterface(legacyClientState) + suite.Require().NoError(err) + clientStore.Set(host.ClientStateKey(), bz) + + any, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) + suite.Require().NoError(err) + suite.Require().NotNil(any) + + // obtain marshalled bytes to set in client store + bz, err = suite.chainA.App.AppCodec().MarshalInterface(legacyClientState.ConsensusState) + suite.Require().NoError(err) + + var consensusStates []clienttypes.ConsensusStateWithHeight + + // set consensus states in store and genesis + for i := uint64(0); i < 10; i++ { + height := clienttypes.NewHeight(1, i) + clientStore.Set(host.ConsensusStateKey(height), bz) + consensusStates = append(consensusStates, clienttypes.ConsensusStateWithHeight{ + Height: height, + ConsensusState: any, + }) + } + + clientGenState.ClientsConsensus = append(clientGenState.ClientsConsensus, clienttypes.ClientConsensusStates{ + ClientId: sm.ClientID, + ConsensusStates: consensusStates, + }) + } + + // solo machine clients must come before tendermint in expected + clientGenState.Clients = append(clients, clientGenState.Clients...) + + // migrate store get expected genesis + // store migration and genesis migration should produce identical results + // NOTE: tendermint clients are not pruned in genesis so the test should not have expired tendermint clients + err := v7.MigrateStore(suite.chainA.GetContext(), suite.chainA.GetSimApp().GetKey(host.StoreKey), suite.chainA.App.AppCodec()) + suite.Require().NoError(err) + expectedClientGenState := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) + + cdc := suite.chainA.App.AppCodec().(*codec.ProtoCodec) + + // NOTE: these lines are added in comparison to 02-client/migrations/v7/genesis_test.go + // generate appState with old ibc genesis state + appState := genutiltypes.AppMap{} + ibcGenState := types.DefaultGenesisState() + ibcGenState.ClientGenesis = clientGenState + clientv7.RegisterInterfaces(cdc.InterfaceRegistry()) + appState[host.ModuleName] = cdc.MustMarshalJSON(ibcGenState) + + // NOTE: genesis time isn't updated since we aren't testing for tendermint consensus state pruning + migrated, err := v7.MigrateGenesis(appState, cdc) + suite.Require().NoError(err) + + expectedAppState := genutiltypes.AppMap{} + expectedIBCGenState := types.DefaultGenesisState() + expectedIBCGenState.ClientGenesis = expectedClientGenState + + bz, err := cdc.MarshalJSON(expectedIBCGenState) + suite.Require().NoError(err) + expectedAppState[host.ModuleName] = bz + + suite.Require().Equal(expectedAppState, migrated) +} diff --git a/modules/core/migrations/v7/migrations.go b/modules/core/migrations/v7/migrations.go deleted file mode 100644 index 8eeda416eb4..00000000000 --- a/modules/core/migrations/v7/migrations.go +++ /dev/null @@ -1,44 +0,0 @@ -package v7 - -import ( - "strings" - - sdk "github.com/cosmos/cosmos-sdk/types" - - clientkeeper "github.com/cosmos/ibc-go/v6/modules/core/02-client/keeper" - clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" - host "github.com/cosmos/ibc-go/v6/modules/core/24-host" - "github.com/cosmos/ibc-go/v6/modules/core/exported" -) - -// Localhost is the client type for a localhost client. It is also used as the clientID -// for the localhost client. -const Localhost string = "09-localhost" - -// MigrateToV7 prunes the 09-Localhost client and associated consensus states from the ibc store -func MigrateToV7(ctx sdk.Context, clientKeeper clientkeeper.Keeper) { - clientStore := clientKeeper.ClientStore(ctx, Localhost) - - iterator := sdk.KVStorePrefixIterator(clientStore, []byte(host.KeyConsensusStatePrefix)) - var heights []exported.Height - - defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { - keySplit := strings.Split(string(iterator.Key()), "/") - // key is in the format "consensusStates/" - if len(keySplit) != 2 || keySplit[0] != string(host.KeyConsensusStatePrefix) { - continue - } - - // collect consensus states to be pruned - heights = append(heights, clienttypes.MustParseHeight(keySplit[1])) - } - - // delete all consensus states - for _, height := range heights { - clientStore.Delete(host.ConsensusStateKey(height)) - } - - // delete the client state - clientStore.Delete(host.ClientStateKey()) -} diff --git a/modules/core/migrations/v7/migrations_test.go b/modules/core/migrations/v7/migrations_test.go deleted file mode 100644 index 53c18ae20d8..00000000000 --- a/modules/core/migrations/v7/migrations_test.go +++ /dev/null @@ -1,116 +0,0 @@ -package v7_test - -import ( - "testing" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/suite" - - clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" - host "github.com/cosmos/ibc-go/v6/modules/core/24-host" - "github.com/cosmos/ibc-go/v6/modules/core/exported" - v7 "github.com/cosmos/ibc-go/v6/modules/core/migrations/v7" - ibctesting "github.com/cosmos/ibc-go/v6/testing" -) - -type MigrationsV7TestSuite struct { - suite.Suite - - coordinator *ibctesting.Coordinator - - chainA *ibctesting.TestChain - chainB *ibctesting.TestChain -} - -func (suite *MigrationsV7TestSuite) SetupTest() { - suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) - - suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1)) - suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2)) -} - -func TestIBCTestSuite(t *testing.T) { - suite.Run(t, new(MigrationsV7TestSuite)) -} - -func (suite *MigrationsV7TestSuite) TestMigrateToV7() { - var clientStore sdk.KVStore - - testCases := []struct { - name string - malleate func() - expPass bool - }{ - { - "success: prune localhost client state", - func() { - clientStore.Set(host.ClientStateKey(), []byte("clientState")) - }, - true, - }, - { - "success: prune localhost client state and consensus states", - func() { - clientStore.Set(host.ClientStateKey(), []byte("clientState")) - - for i := 0; i < 10; i++ { - clientStore.Set(host.ConsensusStateKey(clienttypes.NewHeight(1, uint64(i))), []byte("consensusState")) - } - }, - true, - }, - { - "07-tendermint client state and consensus states remain in client store", - func() { - clientStore = suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clienttypes.FormatClientIdentifier(exported.Tendermint, 0)) - clientStore.Set(host.ClientStateKey(), []byte("clientState")) - - for i := 0; i < 10; i++ { - clientStore.Set(host.ConsensusStateKey(clienttypes.NewHeight(1, uint64(i))), []byte("consensusState")) - } - }, - false, - }, - { - "06-solomachine client state and consensus states remain in client store", - func() { - clientStore = suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clienttypes.FormatClientIdentifier(exported.Solomachine, 0)) - clientStore.Set(host.ClientStateKey(), []byte("clientState")) - - for i := 0; i < 10; i++ { - clientStore.Set(host.ConsensusStateKey(clienttypes.NewHeight(1, uint64(i))), []byte("consensusState")) - } - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - - suite.Run(tc.name, func() { - suite.SetupTest() // reset - - ctx := suite.chainA.GetContext() - clientStore = suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), v7.Localhost) - - tc.malleate() - - v7.MigrateToV7(ctx, suite.chainA.GetSimApp().IBCKeeper.ClientKeeper) - - if tc.expPass { - suite.Require().False(clientStore.Has(host.ClientStateKey())) - - for i := 0; i < 10; i++ { - suite.Require().False(clientStore.Has(host.ConsensusStateKey(clienttypes.NewHeight(1, uint64(i))))) - } - } else { - suite.Require().True(clientStore.Has(host.ClientStateKey())) - - for i := 0; i < 10; i++ { - suite.Require().True(clientStore.Has(host.ConsensusStateKey(clienttypes.NewHeight(1, uint64(i))))) - } - } - }) - } -} From 6790532f5fc9a58d36022576e5195bd6e7d2c4ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:23:18 +0100 Subject: [PATCH 03/12] chore: use conventional unmarshal/marshal type declaration --- modules/core/02-client/migrations/v7/genesis.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go index 86d24960beb..5d4b4a79303 100644 --- a/modules/core/02-client/migrations/v7/genesis.go +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -30,14 +30,14 @@ func MigrateGenesis(clientGenState *types.GenesisState, cdc codec.ProtoCodecMars // update solo machine client state defintions switch clientType { case exported.Solomachine: - clientState := &ClientState{} - if err := cdc.Unmarshal(client.ClientState.Value, clientState); err != nil { + var clientState ClientState + if err := cdc.Unmarshal(client.ClientState.Value, &clientState); err != nil { return nil, sdkerrors.Wrap(err, "failed to unmarshal client state bytes into solo machine client state") } updatedClientState := migrateSolomachine(clientState) - any, err := types.PackClientState(updatedClientState) + any, err := types.PackClientState(&updatedClientState) if err != nil { return nil, err } From 8ec10d90afa8e9eab9ff4df093133a3a4d0647e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:25:33 +0100 Subject: [PATCH 04/12] fix: update codec usage in genesis_test.go --- modules/core/02-client/migrations/v7/genesis_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/core/02-client/migrations/v7/genesis_test.go b/modules/core/02-client/migrations/v7/genesis_test.go index d20d9feda81..203e4652113 100644 --- a/modules/core/02-client/migrations/v7/genesis_test.go +++ b/modules/core/02-client/migrations/v7/genesis_test.go @@ -3,6 +3,7 @@ package v7_test import ( "encoding/json" + "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" ibcclient "github.com/cosmos/ibc-go/v6/modules/core/02-client" @@ -102,10 +103,13 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { suite.Require().NoError(err) expectedClientGenState := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) - migrated, err := v7.MigrateGenesis(&clientGenState, suite.chainA.App.Codec()) + cdc, ok := suite.chainA.App.AppCodec().(codec.ProtoCodecMarshaler) + suite.Require().True(ok) + + migrated, err := v7.MigrateGenesis(&clientGenState, cdc) suite.Require().NoError(err) - bz, err := clientCtx.Codec.MarshalJSON(&expectedClientGenState) + bz, err := cdc.MarshalJSON(&expectedClientGenState) suite.Require().NoError(err) // Indent the JSON bz correctly. @@ -115,7 +119,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { expectedIndentedBz, err := json.MarshalIndent(jsonObj, "", "\t") suite.Require().NoError(err) - bz, err = clientCtx.Codec.MarshalJSON(migrated) + bz, err = cdc.MarshalJSON(migrated) suite.Require().NoError(err) // Indent the JSON bz correctly. From b3af7bbe74c6c1403a296144098a26be910a5fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:29:04 +0100 Subject: [PATCH 05/12] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71629f83a02..647e8512f3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (core/02-client) [\#2824](https://github.com/cosmos/ibc-go/pull/2824) Add genesis migrations for v6 to v7. The migration migrates the solo machine client state definition, removes all solo machine consensus states and removes the localhost client. * (core/02-client) [\#2819](https://github.com/cosmos/ibc-go/pull/2819) Add automatic in-place store migrations to remove the localhost client and migrate existing solo machine definitions. * (light-clients/06-solomachine) [\#2826](https://github.com/cosmos/ibc-go/pull/2826) Add `AppModuleBasic` for the 06-solomachine client and remove solo machine type registration from core IBC. Chains must register the `AppModuleBasic` of light clients. * (light-clients/07-tendermint) [\#2825](https://github.com/cosmos/ibc-go/pull/2825) Add `AppModuleBasic` for the 07-tendermint client and remove tendermint type registration from core IBC. Chains must register the `AppModuleBasic` of light clients. From 1d72430760f8bb03a4998cad5cb48276ba66b497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 1 Dec 2022 12:20:13 +0100 Subject: [PATCH 06/12] chore: improve test code readability --- modules/core/02-client/migrations/v7/genesis_test.go | 2 ++ modules/core/migrations/v7/genesis_test.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/modules/core/02-client/migrations/v7/genesis_test.go b/modules/core/02-client/migrations/v7/genesis_test.go index 203e4652113..188df383eb9 100644 --- a/modules/core/02-client/migrations/v7/genesis_test.go +++ b/modules/core/02-client/migrations/v7/genesis_test.go @@ -22,6 +22,8 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { err := path.EndpointA.UpdateClient() suite.Require().NoError(err) + + // update a second time to add more state err = path.EndpointA.UpdateClient() suite.Require().NoError(err) } diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go index 428754fe99b..3cc8d3e5704 100644 --- a/modules/core/migrations/v7/genesis_test.go +++ b/modules/core/migrations/v7/genesis_test.go @@ -49,6 +49,8 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { err := path.EndpointA.UpdateClient() suite.Require().NoError(err) + + // update a second time to add more state err = path.EndpointA.UpdateClient() suite.Require().NoError(err) } From 30d492e49a9cf3e50c69a1da3f8347292c9d494d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 1 Dec 2022 12:24:11 +0100 Subject: [PATCH 07/12] fix: updated tests to account for changes --- modules/core/migrations/v7/genesis_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go index 3cc8d3e5704..fa32f23961e 100644 --- a/modules/core/migrations/v7/genesis_test.go +++ b/modules/core/migrations/v7/genesis_test.go @@ -70,9 +70,9 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { clientState := sm.ClientState() // generate old client state proto definition - legacyClientState := &v7.ClientState{ + legacyClientState := &clientv7.ClientState{ Sequence: clientState.Sequence, - ConsensusState: &v7.ConsensusState{ + ConsensusState: &clientv7.ConsensusState{ PublicKey: clientState.ConsensusState.PublicKey, Diversifier: clientState.ConsensusState.Diversifier, Timestamp: clientState.ConsensusState.Timestamp, @@ -128,7 +128,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { // migrate store get expected genesis // store migration and genesis migration should produce identical results // NOTE: tendermint clients are not pruned in genesis so the test should not have expired tendermint clients - err := v7.MigrateStore(suite.chainA.GetContext(), suite.chainA.GetSimApp().GetKey(host.StoreKey), suite.chainA.App.AppCodec()) + err := clientv7.MigrateStore(suite.chainA.GetContext(), suite.chainA.GetSimApp().GetKey(host.StoreKey), suite.chainA.App.AppCodec()) suite.Require().NoError(err) expectedClientGenState := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) From fa055086d8859801a1e193170f611ba0b2d66e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:39:37 +0100 Subject: [PATCH 08/12] chore: rename types to clienttypes --- modules/core/02-client/migrations/v7/genesis.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go index 5d4b4a79303..f7a689488cf 100644 --- a/modules/core/02-client/migrations/v7/genesis.go +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -4,7 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" + clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" "github.com/cosmos/ibc-go/v6/modules/core/exported" ) @@ -13,16 +13,16 @@ import ( // - Update solo machine client state protobuf definition (v2 to v3) // - Remove all solo machine consensus states // - Remove localhost client -func MigrateGenesis(clientGenState *types.GenesisState, cdc codec.ProtoCodecMarshaler) (*types.GenesisState, error) { +func MigrateGenesis(clientGenState *clienttypes.GenesisState, cdc codec.ProtoCodecMarshaler) (*clienttypes.GenesisState, error) { // To prune the client and consensus states, we will create new slices to fill up // with information we want to keep. var ( - clientsConsensus []types.ClientConsensusStates - clients []types.IdentifiedClientState + clientsConsensus []clienttypes.ClientConsensusStates + clients []clienttypes.IdentifiedClientState ) for _, client := range clientGenState.Clients { - clientType, _, err := types.ParseClientIdentifier(client.ClientId) + clientType, _, err := clienttypes.ParseClientIdentifier(client.ClientId) if err != nil { return nil, err } @@ -37,12 +37,12 @@ func MigrateGenesis(clientGenState *types.GenesisState, cdc codec.ProtoCodecMars updatedClientState := migrateSolomachine(clientState) - any, err := types.PackClientState(&updatedClientState) + any, err := clienttypes.PackClientState(&updatedClientState) if err != nil { return nil, err } - clients = append(clients, types.IdentifiedClientState{ + clients = append(clients, clienttypes.IdentifiedClientState{ ClientId: client.ClientId, ClientState: any, }) From 7c9ebc04e8f1ad43132cecc1fb5a34387f4fba82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:42:40 +0100 Subject: [PATCH 09/12] chore: combine solomachine/localhost in switch statement no-op --- modules/core/02-client/migrations/v7/genesis.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go index f7a689488cf..964ff7b30f5 100644 --- a/modules/core/02-client/migrations/v7/genesis.go +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -60,12 +60,8 @@ func MigrateGenesis(clientGenState *clienttypes.GenesisState, cdc codec.ProtoCod // look for consensus states for the current client if clientConsensusStates.ClientId == client.ClientId { switch clientType { - case exported.Solomachine: - // remove all consensus states for the solo machine - // do not add to new clientsConsensus - - case Localhost: - // remove all consensus states for the solo machine + case exported.Solomachine, Localhost: + // remove all consensus states for the solo machine and localhost // do not add to new clientsConsensus default: From 53ea7af36ebef4d89c64e7caea83801b1a49dc61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:45:02 +0100 Subject: [PATCH 10/12] chore: rename any var to protoAny --- modules/core/02-client/migrations/v7/genesis.go | 4 ++-- modules/core/02-client/migrations/v7/genesis_test.go | 12 ++++++------ modules/core/migrations/v7/genesis_test.go | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go index 964ff7b30f5..daf29d56498 100644 --- a/modules/core/02-client/migrations/v7/genesis.go +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -37,14 +37,14 @@ func MigrateGenesis(clientGenState *clienttypes.GenesisState, cdc codec.ProtoCod updatedClientState := migrateSolomachine(clientState) - any, err := clienttypes.PackClientState(&updatedClientState) + protoAny, err := clienttypes.PackClientState(&updatedClientState) if err != nil { return nil, err } clients = append(clients, clienttypes.IdentifiedClientState{ ClientId: client.ClientId, - ClientState: any, + ClientState: protoAny, }) case Localhost: diff --git a/modules/core/02-client/migrations/v7/genesis_test.go b/modules/core/02-client/migrations/v7/genesis_test.go index 188df383eb9..990fc7d367e 100644 --- a/modules/core/02-client/migrations/v7/genesis_test.go +++ b/modules/core/02-client/migrations/v7/genesis_test.go @@ -54,13 +54,13 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { } // set client state - any, err := codectypes.NewAnyWithValue(legacyClientState) + protoAny, err := codectypes.NewAnyWithValue(legacyClientState) suite.Require().NoError(err) - suite.Require().NotNil(any) + suite.Require().NotNil(protoAny) clients = append(clients, types.IdentifiedClientState{ ClientId: sm.ClientID, - ClientState: any, + ClientState: protoAny, }) // set in store for ease of determining expected genesis @@ -69,9 +69,9 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { suite.Require().NoError(err) clientStore.Set(host.ClientStateKey(), bz) - any, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) + protoAny, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) suite.Require().NoError(err) - suite.Require().NotNil(any) + suite.Require().NotNil(protoAny) // obtain marshalled bytes to set in client store bz, err = suite.chainA.App.AppCodec().MarshalInterface(legacyClientState.ConsensusState) @@ -85,7 +85,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { clientStore.Set(host.ConsensusStateKey(height), bz) consensusStates = append(consensusStates, types.ConsensusStateWithHeight{ Height: height, - ConsensusState: any, + ConsensusState: protoAny, }) } diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go index fa32f23961e..c3323ebd4de 100644 --- a/modules/core/migrations/v7/genesis_test.go +++ b/modules/core/migrations/v7/genesis_test.go @@ -81,13 +81,13 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { } // set client state - any, err := codectypes.NewAnyWithValue(legacyClientState) + protoAny, err := codectypes.NewAnyWithValue(legacyClientState) suite.Require().NoError(err) - suite.Require().NotNil(any) + suite.Require().NotNil(protoAny) clients = append(clients, clienttypes.IdentifiedClientState{ ClientId: sm.ClientID, - ClientState: any, + ClientState: protoAny, }) // set in store for ease of determining expected genesis @@ -96,9 +96,9 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { suite.Require().NoError(err) clientStore.Set(host.ClientStateKey(), bz) - any, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) + protoAny, err = codectypes.NewAnyWithValue(legacyClientState.ConsensusState) suite.Require().NoError(err) - suite.Require().NotNil(any) + suite.Require().NotNil(protoAny) // obtain marshalled bytes to set in client store bz, err = suite.chainA.App.AppCodec().MarshalInterface(legacyClientState.ConsensusState) @@ -112,7 +112,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { clientStore.Set(host.ConsensusStateKey(height), bz) consensusStates = append(consensusStates, clienttypes.ConsensusStateWithHeight{ Height: height, - ConsensusState: any, + ConsensusState: protoAny, }) } From b558915a3e7a99b6df2abe69c0f7be0655626649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:53:03 +0100 Subject: [PATCH 11/12] chore: apply review suggestions to improve code readability --- modules/core/migrations/v7/genesis.go | 34 ++++++++++++---------- modules/core/migrations/v7/genesis_test.go | 2 ++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/modules/core/migrations/v7/genesis.go b/modules/core/migrations/v7/genesis.go index 0be575aa234..356589fd3d3 100644 --- a/modules/core/migrations/v7/genesis.go +++ b/modules/core/migrations/v7/genesis.go @@ -15,26 +15,28 @@ import ( // - Remove all solo machine consensus states // - Remove any localhost clients func MigrateGenesis(appState genutiltypes.AppMap, cdc codec.ProtoCodecMarshaler) (genutiltypes.AppMap, error) { - if appState[host.ModuleName] != nil { - // ensure legacy solo machines are registered - clientv7.RegisterInterfaces(cdc.InterfaceRegistry()) + if appState[host.ModuleName] == nil { + return appState, nil + } + + // ensure legacy solo machines types are registered + clientv7.RegisterInterfaces(cdc.InterfaceRegistry()) - // unmarshal relative source genesis application state - ibcGenState := &types.GenesisState{} - cdc.MustUnmarshalJSON(appState[host.ModuleName], ibcGenState) + // unmarshal old ibc genesis state + ibcGenState := &types.GenesisState{} + cdc.MustUnmarshalJSON(appState[host.ModuleName], ibcGenState) - clientGenState, err := clientv7.MigrateGenesis(&ibcGenState.ClientGenesis, cdc) - if err != nil { - return nil, err - } + clientGenState, err := clientv7.MigrateGenesis(&ibcGenState.ClientGenesis, cdc) + if err != nil { + return nil, err + } - ibcGenState.ClientGenesis = *clientGenState + ibcGenState.ClientGenesis = *clientGenState - // delete old genesis state - delete(appState, host.ModuleName) + // delete old genesis state + delete(appState, host.ModuleName) - // set new ibc genesis state - appState[host.ModuleName] = cdc.MustMarshalJSON(ibcGenState) - } + // set new ibc genesis state + appState[host.ModuleName] = cdc.MustMarshalJSON(ibcGenState) return appState, nil } diff --git a/modules/core/migrations/v7/genesis_test.go b/modules/core/migrations/v7/genesis_test.go index c3323ebd4de..1ba9fcdf48d 100644 --- a/modules/core/migrations/v7/genesis_test.go +++ b/modules/core/migrations/v7/genesis_test.go @@ -139,6 +139,8 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() { appState := genutiltypes.AppMap{} ibcGenState := types.DefaultGenesisState() ibcGenState.ClientGenesis = clientGenState + + // ensure tests pass even if the legacy solo machine is already registered clientv7.RegisterInterfaces(cdc.InterfaceRegistry()) appState[host.ModuleName] = cdc.MustMarshalJSON(ibcGenState) From 83f851c13db15e735897a80d868e40c22ef52985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:58:03 +0100 Subject: [PATCH 12/12] chore: remove unnecessary comment --- modules/core/02-client/migrations/v7/genesis.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/core/02-client/migrations/v7/genesis.go b/modules/core/02-client/migrations/v7/genesis.go index daf29d56498..b1071f1a4f4 100644 --- a/modules/core/02-client/migrations/v7/genesis.go +++ b/modules/core/02-client/migrations/v7/genesis.go @@ -27,7 +27,6 @@ func MigrateGenesis(clientGenState *clienttypes.GenesisState, cdc codec.ProtoCod return nil, err } - // update solo machine client state defintions switch clientType { case exported.Solomachine: var clientState ClientState