From 49e4d059ae14988e2d0a091c37fa30bd2e1c81b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Mon, 24 Aug 2020 20:37:11 +0200 Subject: [PATCH] Allow localhost to be created (#7148) * revert localhost not being created * Update x/ibc/02-client/keeper/grpc_query_test.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- x/ibc/02-client/abci_test.go | 16 +++++------- x/ibc/02-client/genesis.go | 2 ++ x/ibc/02-client/handler_test.go | 6 ----- x/ibc/02-client/keeper/client_test.go | 6 ++--- x/ibc/02-client/keeper/grpc_query_test.go | 32 ++++++++++++++--------- x/ibc/02-client/keeper/keeper_test.go | 25 ++++++++++++------ x/ibc/02-client/types/genesis.go | 2 +- 7 files changed, 50 insertions(+), 39 deletions(-) diff --git a/x/ibc/02-client/abci_test.go b/x/ibc/02-client/abci_test.go index f19d472e5..b924f007d 100644 --- a/x/ibc/02-client/abci_test.go +++ b/x/ibc/02-client/abci_test.go @@ -5,6 +5,8 @@ import ( "github.com/stretchr/testify/suite" + client "github.com/cosmos/cosmos-sdk/x/ibc/02-client" + "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing" ) @@ -28,26 +30,22 @@ func TestClientTestSuite(t *testing.T) { suite.Run(t, new(ClientTestSuite)) } -/* TODO: uncomment once simapp is switched to proto func (suite *ClientTestSuite) TestBeginBlocker() { - prevHeight := suite.ctx.BlockHeight() + prevHeight := suite.chainA.GetContext().BlockHeight() - localHostClient, found := suite.app.IBCKeeper.ClientKeeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost) - suite.Require().True(found) + localHostClient := suite.chainA.GetClientState(exported.ClientTypeLocalHost) suite.Require().Equal(prevHeight, int64(localHostClient.GetLatestHeight())) for i := 0; i < 10; i++ { // increment height - suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1) + suite.coordinator.CommitBlock(suite.chainA, suite.chainB) suite.Require().NotPanics(func() { - client.BeginBlocker(suite.ctx, suite.app.IBCKeeper.ClientKeeper) + client.BeginBlocker(suite.chainA.GetContext(), suite.chainA.App.IBCKeeper.ClientKeeper) }, "BeginBlocker shouldn't panic") - localHostClient, found = suite.app.IBCKeeper.ClientKeeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost) - suite.Require().True(found) + localHostClient = suite.chainA.GetClientState(exported.ClientTypeLocalHost) suite.Require().Equal(prevHeight+1, int64(localHostClient.GetLatestHeight())) prevHeight = int64(localHostClient.GetLatestHeight()) } } -*/ diff --git a/x/ibc/02-client/genesis.go b/x/ibc/02-client/genesis.go index 710e90469..8977c4e37 100644 --- a/x/ibc/02-client/genesis.go +++ b/x/ibc/02-client/genesis.go @@ -52,6 +52,8 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) { } // ExportGenesis returns the ibc client submodule's exported genesis. +// NOTE: CreateLocalhost should always be false on export since a +// created localhost will be included in the exported clients. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { return types.GenesisState{ Clients: k.GetAllGenesisClients(ctx), diff --git a/x/ibc/02-client/handler_test.go b/x/ibc/02-client/handler_test.go index e500849be..eec25ff36 100644 --- a/x/ibc/02-client/handler_test.go +++ b/x/ibc/02-client/handler_test.go @@ -13,12 +13,6 @@ func (suite *ClientTestSuite) TestHandleCreateClientLocalHost() { msg exported.MsgCreateClient expPass bool }{ - { - "localhost", - exported.ClientTypeLocalHost, - &localhosttypes.MsgCreateClient{suite.chainA.SenderAccount.GetAddress()}, - true, - }, { "tendermint client", "gaiamainnet", diff --git a/x/ibc/02-client/keeper/client_test.go b/x/ibc/02-client/keeper/client_test.go index 817d40586..eed36d568 100644 --- a/x/ibc/02-client/keeper/client_test.go +++ b/x/ibc/02-client/keeper/client_test.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" + localhosttypes "github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" ) @@ -229,16 +230,15 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { } } -/* func (suite *KeeperTestSuite) TestUpdateClientLocalhost() { - var localhostClient exported.ClientState = localhosttypes.NewClientState(suite.header.ChainID, suite.ctx.BlockHeight()) + var localhostClient exported.ClientState = localhosttypes.NewClientState(suite.header.Header.GetChainID(), suite.ctx.BlockHeight()) suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1) updatedClientState, err := suite.keeper.UpdateClient(suite.ctx, exported.ClientTypeLocalHost, nil) suite.Require().NoError(err, err) suite.Require().Equal(localhostClient.GetLatestHeight()+1, updatedClientState.GetLatestHeight()) -}*/ +} func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() { altPrivVal := tmtypes.NewMockPV() diff --git a/x/ibc/02-client/keeper/grpc_query_test.go b/x/ibc/02-client/keeper/grpc_query_test.go index 24a7bb188..dcc8c4c43 100644 --- a/x/ibc/02-client/keeper/grpc_query_test.go +++ b/x/ibc/02-client/keeper/grpc_query_test.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" + "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types" ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" @@ -110,19 +111,20 @@ func (suite *KeeperTestSuite) TestQueryClientStates() { { "success", func() { - clientState := ibctmtypes.NewClientState(testChainID, ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, 0, commitmenttypes.GetSDKSpecs()) - clientState2 := ibctmtypes.NewClientState(testChainID, ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, 0, commitmenttypes.GetSDKSpecs()) - suite.keeper.SetClientState(suite.ctx, testClientID, clientState) - suite.keeper.SetClientState(suite.ctx, testClientID2, clientState2) + clientA1, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + clientA2, _ := suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint) - idcs := types.NewIdentifiedClientState(testClientID, clientState) - idcs2 := types.NewIdentifiedClientState(testClientID2, clientState2) + clientStateA1 := suite.chainA.GetClientState(clientA1) + clientStateA2 := suite.chainA.GetClientState(clientA2) - // order is swapped because the res is sorted by client id - expClientStates = []*types.IdentifiedClientState{&idcs2, &idcs} + idcs := types.NewIdentifiedClientState(clientA1, clientStateA1) + idcs2 := types.NewIdentifiedClientState(clientA2, clientStateA2) + + // order is sorted by client id, localhost is last + expClientStates = []*types.IdentifiedClientState{&idcs, &idcs2} req = &types.QueryClientStatesRequest{ Pagination: &query.PageRequest{ - Limit: 3, + Limit: 7, CountTotal: true, }, } @@ -134,11 +136,18 @@ func (suite *KeeperTestSuite) TestQueryClientStates() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset + expClientStates = nil tc.malleate() - ctx := sdk.WrapSDKContext(suite.ctx) - res, err := suite.queryClient.ClientStates(ctx, req) + // always add localhost which is created by default in init genesis + localhostClientState := suite.chainA.GetClientState(exported.Localhost.String()) + identifiedLocalhost := types.NewIdentifiedClientState(exported.Localhost.String(), localhostClientState) + expClientStates = append(expClientStates, &identifiedLocalhost) + + ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) + + res, err := suite.chainA.QueryServer.ClientStates(ctx, req) if tc.expPass { suite.Require().NoError(err) @@ -147,7 +156,6 @@ func (suite *KeeperTestSuite) TestQueryClientStates() { for i := range expClientStates { suite.Require().Equal(expClientStates[i].ClientId, res.ClientStates[i].ClientId) suite.Require().NotNil(res.ClientStates[i].ClientState) - expClientStates[i].ClientState.ClearCachedValue() suite.Require().Equal(expClientStates[i].ClientState, res.ClientStates[i].ClientState) } } else { diff --git a/x/ibc/02-client/keeper/keeper_test.go b/x/ibc/02-client/keeper/keeper_test.go index 5809c3f15..2e277b3fa 100644 --- a/x/ibc/02-client/keeper/keeper_test.go +++ b/x/ibc/02-client/keeper/keeper_test.go @@ -21,6 +21,7 @@ import ( ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" localhosttypes "github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" + ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -41,6 +42,11 @@ const ( type KeeperTestSuite struct { suite.Suite + coordinator *ibctesting.Coordinator + + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain + cdc codec.Marshaler ctx sdk.Context keeper *keeper.Keeper @@ -56,6 +62,11 @@ type KeeperTestSuite struct { } func (suite *KeeperTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) + isCheckTx := false suite.now = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC) suite.past = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) @@ -207,10 +218,9 @@ func (suite KeeperTestSuite) TestGetAllClients() { } // add localhost client - // TODO: uncomment after simapp is migrated to proto - // localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost) - // suite.Require().True(found) - // expClients = append(expClients, localHostClient) + localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost) + suite.Require().True(found) + expClients = append(expClients, localHostClient) clients := suite.keeper.GetAllClients(suite.ctx) suite.Require().Len(clients, len(expClients)) @@ -235,10 +245,9 @@ func (suite KeeperTestSuite) TestGetAllGenesisClients() { } // add localhost client - // TODO: uncomment after simapp is migrated to proto - // localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost) - // suite.Require().True(found) - // expGenClients = append(expGenClients, types.NewIdentifiedClientState(exported.ClientTypeLocalHost, localHostClient)) + localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost) + suite.Require().True(found) + expGenClients = append(expGenClients, types.NewIdentifiedClientState(exported.ClientTypeLocalHost, localHostClient)) genClients := suite.keeper.GetAllGenesisClients(suite.ctx) diff --git a/x/ibc/02-client/types/genesis.go b/x/ibc/02-client/types/genesis.go index f5b29c5d0..3b0fa173c 100644 --- a/x/ibc/02-client/types/genesis.go +++ b/x/ibc/02-client/types/genesis.go @@ -88,7 +88,7 @@ func DefaultGenesisState() GenesisState { return GenesisState{ Clients: []IdentifiedClientState{}, ClientsConsensus: ClientsConsensusStates{}, - CreateLocalhost: false, + CreateLocalhost: true, } }